Vue和微信小程序的对比分析
Vue和微信小程序的对比分析
Vue和微信小程序是前端开发中常用的两个技术栈,它们在很多方面都有相似之处,但也存在一些关键性的差异。本文将从生命周期、数据绑定、列表渲染等多个维度,详细对比分析Vue和微信小程序的异同点。
一、生命周期
Vue和微信小程序在生命周期管理上有一些差异。Vue的生命周期钩子函数相对复杂,涵盖了从创建到销毁的多个阶段,而微信小程序的生命周期则相对简洁。
Vue生命周期
相比之下,微信小程序的钩子函数要简单得多。Vue的钩子函数在跳转新页面时,钩子函数都会触发,但是微信小程序的钩子函数,页面不同的跳转方式,触发的钩子并不一样。
onLoad
: 页面加载
一个页面只会调用一次,可以在onLoad
中获取打开当前页面所调用的query
参数。onShow
: 页面显示
每次打开页面都会调用一次。onReady
: 页面初次渲染完成
一个页面只会调用一次,代表页面已经准备妥当,可以和视图层进行交互。
对界面的设置如wx.setNavigationBarTitle
请在onReady
之后设置。详见生命周期onHide
: 页面隐藏
当navigateTo
或底部tab切换时调用。onUnload
: 页面卸载
当redirectTo
或navigateBack
的时候调用。
数据请求
在页面加载请求数据时,两者钩子的使用有些类似,Vue一般会在 created
或者 mounted
中请求数据,而在微信小程序,会在 onLoad
或者 onShow
中请求数据。
二、数据绑定
Vue和微信小程序在数据绑定的语法上有所不同。
Vue:Vue动态绑定一个变量的值为元素的某个属性的时候,会在变量前面加上冒号
:
,例如:<img :src="imgSrc"/>
微信小程序:绑定某个变量的值为元素属性时,会用两个大括号括起来,如果不加括号,会被认为是字符串。例如:
<image src="{{imgSrc}}"></image>
三、列表渲染
Vue和微信小程序在列表渲染上的语法相似,但具体实现方式有所不同。
Vue
<ul id="example-1">
<li v-for="item in items">
{{ item.message }}
</li>
</ul>
<script>
var example1 = new Vue({
el: '#example-1',
data: {
items: [
{ message: 'Foo' },
{ message: 'Bar' }
]
}
})
</script>
微信小程序
Page({
data: {
items: [
{ message: 'Foo' },
{ message: 'Bar' }
]
}
})
<text wx:for="{{items}}">{{item}}</text>
四、显示与隐藏元素
Vue和微信小程序在控制元素显示隐藏的方式上有所不同。
- Vue:使用
v-if
和v-show
控制元素的显示和隐藏 - 微信小程序:使用
wx-if
和hidden
控制元素的显示和隐藏
五、事件处理
Vue和微信小程序在事件处理的语法上有所不同。
Vue
使用 v-on:event
绑定事件,或者使用 @event
绑定事件,例如:
<button v-on:click="counter += 1">Add 1</button>
<button v-on:click.stop="counter+=1">Add1</button> //阻止事件冒泡
微信小程序
全用 bindtap(bind+event)
,或者 catchtap(catch+event)
绑定事件,例如:
<button bindtap="noWork">明天不上班</button>
<button catchtap="noWork">明天不上班</button> //阻止事件冒泡
六、数据双向绑定
1. 设置值
在Vue中,只需要在表单元素上加上 v-model
,然后再绑定 data
中对应的一个值,当表单元素内容发生变化时,data
中对应的值也会相应改变,这是Vue非常nice的一点。
<div id="app">
<input v-model="reason" placeholder="填写理由" class='reason'/>
</div>
<script>
new Vue({
el: '#app',
data: {
reason:''
}
})
</script>
但是在微信小程序中,却没有这个功能。那怎么办呢?
当表单内容发生变化时,会触发表单元素上绑定的方法,然后在该方法中,通过 this.setData({key:value})
来将表单上的值赋值给 data
中的对应值。
下面是代码,可以感受一下:
<input bindinput="bindReason" placeholder="填写理由" class='reason' value='{{reason}}' name="reason" />
<script>
Page({
data:{
reason:''
},
bindReason(e) {
this.setData({
reason: e.detail.value
})
}
})
</script>
2. 取值
- Vue中,通过
this.reason
取值 - 微信小程序中,通过
this.data.reason
取值
七、绑定事件传参
在Vue中,绑定事件传参挺简单,只需要在触发事件的方法中,把需要传递的数据作为形参传入就可以了,例如:
<button @click="say('明天不上班')"></button>
<script>
new Vue({
el: '#app',
methods:{
say(arg){
console.log(arg)
}
}
})
</script>
在微信小程序中,不能直接在绑定事件的方法中传入参数,需要将参数作为属性值,绑定到元素上的 data-
属性上,然后在方法中,通过 e.currentTarget.dataset.*
的方式获取,从而完成参数的传递,很麻烦有没有...
<view class='tr' bindtap='toApprove' data-id="{{item.id}}"></view>
<script>
Page({
data:{
reason:''
},
toApprove(e) {
let id = e.currentTarget.dataset.id;
}
})
</script>
八、父子组件通信
1. 子组件的使用
在Vue中,需要:
- 编写子组件
- 在需要使用的父组件中通过
import
引入 - 在Vue的
components
中注册 - 在模板中使用
//子组件 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(){
return{
title:"我是标题"
}
},
methods:{
helloWorld(){
console.log('我接收到子组件传递的事件了')
}
},
components:{
Bar
}
}
</script>
在微信小程序中,需要:
- 编写子组件
- 在子组件的
json
文件中,将该文件声明为组件{ "component": true }
- 在需要引入的父组件的
json
文件中,在usingComponents
填写引入组件的组件名以及路径"usingComponents": { "tab-bar": "../../components/tabBar/tabBar" }
- 在父组件中,直接引入即可
<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(){
return{
title:"我是标题"
}
},
components:{
Bar
}
}
</script>
// 子组件bar.vue
<template>
<div class="search-box">
<div :title="title" ></div>
</div>
</template>
<script>
export default{
props:{
title:{
type:String,
default:''
}
}
}
</script>
子组件和父组件通信可以通过 this.$emit
将方法和数据传递给父组件。
在微信小程序中
父组件向子组件通信和Vue类似,但是微信小程序没有通过 v-bind
,而是直接将值赋值给一个变量,如下:
<tab-bar currentpage="index"></tab-bar>
此处, “index”就是要向子组件传递的值
在子组件 properties
中,接收传递的值
properties: {
// 弹窗标题
currentpage: { // 属性名
type: String, // 类型(必填),目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型)
value: 'index' // 属性初始值(可选),如果未指定则会根据类型选择一个
}
}
//子组件中
methods: {
// 传递给父组件
cancelBut: function (e) {
var that = this;
var myEventDetail = { pickerShow: false, type: 'cancel' } // detail对象,提供给事件监听函数
this.triggerEvent('myevent', myEventDetail) //myevent自定义名称事件,父组件中使用
},
}
//父组件中
<bar bind:myevent="toggleToast"></bar>
// 获取子组件信息
toggleToast(e){
console.log(e.detail)
}
如果父组件想要调用子组件的方法
Vue会给子组件添加一个 ref
属性,通过 this.$refs.ref的值
便可以获取到该子组件,然后便可以调用子组件中的任意方法,例如
//子组件
<bar ref="bar"></bar>
//父组件
this.$ref.bar.子组件的方法
微信小程序是给子组件添加 id
或者 class
,然后通过 this.selectComponent
找到子组件,然后再调用子组件的方法,示例:
//子组件
<bar id="bar"></bar>
// 父组件
this.selectComponent('#id').syaHello()
小程序父组件改变子组件样式
- 父组件将style传入子组件
- 父组件传入变量控制子组件样式
- 在父组件样式中,在子组件类名前面加上父组件类名
<view class='share-button-container' bindtap='handleShareBtn'>
<share-button product="{{goodProduct}}" type="1" back-color="#fff" fore-color="#9e292f" bind:error="on_error" />
</view>
.share-button-container .button--btn-navigator__hover{
background: #fff;
}