浅析Vue 和微信小程序的区别、比较

2020-06-14 06:19:12易采站长站整理

2、在需要使用的父组件中通过 import引入

3、在 vue的 components中注册

4、在模板中使用


//子组件 bar.vue
<template>
<div class="search-box">
<div @click="say" :title="title" class="icon-dismiss"></div>
</div>
</template>
<script>
export default{
props:{
title:{
type:String,
default:''
}
}
},
methods:{
say(){
console.log('明天不上班');
this.$emit('helloWorld')
}
}
</script>

// 父组件 foo.vue
<template>
<div class="container">
<bar :title="title" @helloWorld="helloWorld"></bar>
</div>
</template>

<script>
import Bar from './bar.vue'
export default{
data:{
title:"我是标题"
},
methods:{
helloWorld(){
console.log('我接收到子组件传递的事件了')
}
},
components:{
Bar
}
</script>

在小程序中,需要:

1、编写子组件

2、在子组件的 json文件中,将该文件声明为组件


{
"component": true
}

3、在需要引入的父组件的 json文件中,在 usingComponents填写引入组件的组件名以及路径


"usingComponents": {
"tab-bar": "../../components/tabBar/tabBar"
}

4、在父组件中,直接引入即可


<tab-bar currentpage="index"></tab-bar>

具体代码:


// 子组件
<!--components/tabBar/tabBar.wxml-->
<view class='tabbar-wrapper'>
<view class='left-bar {{currentpage==="index"?"active":""}}' bindtap='jumpToIndex'>
<text class='iconfont icon-shouye'></text>
<view>首页</view>
</view>
<view class='right-bar {{currentpage==="setting"?"active":""}}' bindtap='jumpToSetting'>
<text class='iconfont icon-shezhi'></text>
<view>设置</view>
</view>
</view>

2、父子组件间通信

在vue中

父组件向子组件传递数据,只需要在子组件通过 v-bind传入一个值,在子组件中,通过 props接收,即可完成数据的传递,示例:


// 父组件 foo.vue
<template>
<div class="container">
<bar :title="title"></bar>
</div>
</template>
<script>
import Bar from './bar.vue'
export default{
data:{
title:"我是标题"
},
components:{
Bar
}
</script>

// 子组件bar.vue