|
寫了vue項目和小(xiao)程序,發現二者有(you)許多相(xiang)同(tong)之處,在(zai)此想總(zong)結一(yi)下二者的共(gong)同(tong)點和區別。 一、生命周期先貼兩(liang)張圖: vue生命周期
小程序(xu)生命周期(qi)
相(xiang)比之下(xia),小程序的鉤子函數要簡單(dan)得多(duo)。 vue的鉤(gou)子(zi)函數在(zai)跳(tiao)轉新頁(ye)面時(shi),鉤(gou)子(zi)函數都會觸(chu)發(fa),但(dan)是(shi)小程序的鉤(gou)子(zi)函數,頁(ye)面不(bu)同的跳(tiao)轉方式(shi),觸(chu)發(fa)的鉤(gou)子(zi)并不(bu)一樣(yang)。
一個(ge)頁(ye)面(mian)(mian)只會(hui)調用(yong)一次(ci),可以在 onLoad 中獲(huo)取(qu)打開當前(qian)頁(ye)面(mian)(mian)所調用(yong)的 query 參(can)數。
每次(ci)打開頁面都會(hui)調用一(yi)次(ci)。
一(yi)個(ge)頁面只會調(diao)用(yong)一(yi)次,代表頁面已經準備妥當,可以(yi)和視圖層進(jin)行(xing)交互。 對界面的設置(zhi)如wx.setNavigationBarTitle請(qing)在(zai)onReady之后設置(zhi)。詳(xiang)見(jian)生命周期
當navigateTo或底(di)部tab切換時調用。
當redirectTo或navigateBack的時候調(diao)用。 數(shu)據請求 在(zai)(zai)頁面(mian)加載請求數(shu)據時,兩者(zhe)(zhe)鉤子的(de)使用有些(xie)類(lei)似,vue一(yi)般會在(zai)(zai)created或(huo)者(zhe)(zhe)mounted中請求數(shu)據,而在(zai)(zai)小程序,會在(zai)(zai)onLoad或(huo)者(zhe)(zhe)onShow中請求數(shu)據。 二、數據綁定VUE:vue動(dong)態綁定一個(ge)變(bian)量的值為元素的某個(ge)屬性的時候,會(hui)在(zai)變(bian)量前(qian)面(mian)加(jia)上冒(mao)號:,例(li): <img :src="imgSrc"/> 小程序:綁定某個(ge)變量的值(zhi)為(wei)(wei)元素屬性時,會用兩個(ge)大括號括起來,如果不加括號,為(wei)(wei)被(bei)認為(wei)(wei)是字符(fu)串。例(li):
<image src="{{imgSrc}}"></image>
三、列表渲染直(zhi)接貼代(dai)碼,兩(liang)者(zhe)還是有些相似(si) vue:
<ul id="example-1">
<li v-for="item in items">
{{ item.message }}
</li>
</ul>
var example1 = new Vue({
el: '#example-1',
data: {
items: [
{ message: 'Foo' },
{ message: 'Bar' }
]
}
})
小程序:
Page({
data: {
items: [
{ message: 'Foo' },
{ message: 'Bar' }
]
}
})
<text wx:for="{{items}}">{{item}}</text>
四、顯示與隱藏元素vue中,使用v-if 和v-show控制元素的顯示和隱(yin)藏 小程序中(zhong),使用wx-if和hidden控制(zhi)元素的顯示和隱藏 五、事件處理vue:使(shi)用v-on:event綁定事件(jian),或者使(shi)用@event綁定事件(jian),例如: <button v-on:click="counter += 1">Add 1</button> <button v-on:click.stop="counter+=1">Add1</button> //阻止事件冒泡 小程(cheng)序中,全用bindtap(bind+event),或(huo)者(zhe)catchtap(catch+event)綁定事件,例如: <button bindtap="noWork">明天不上班</button> <button catchtap="noWork">明天不上班</button> //阻止事件冒泡 六、數據雙向綁定1.設置值在vue中(zhong)(zhong),只需要再表單元(yuan)素(su)上加上v-model,然后再綁定(ding)data中(zhong)(zhong)對(dui)應的一個值(zhi),當表單元(yuan)素(su)內容發生變(bian)化時(shi),data中(zhong)(zhong)對(dui)應的值(zhi)也會相應改(gai)變(bian),這(zhe)是vue非常nice的一點。
<div id="app">
<input v-model="reason" placeholder="填寫理由" class='reason'/>
</div>
new Vue({
el: '#app',
data: {
reason:''
}
})
但是在小程序(xu)中,卻沒有這個功能。那(nei)怎么(me)辦呢? 當表(biao)單內容發(fa)生變化時,會(hui)觸發(fa)表(biao)單元素上綁定的(de)方法,然后在該方法中,通過this.setData({key:value})來將(jiang)表(biao)單上的(de)值(zhi)賦值(zhi)給data中的(de)對(dui)應(ying)值(zhi)。 下面(mian)是代(dai)碼,可以感受(shou)一(yi)下:
<input bindinput="bindReason" placeholder="填寫理由" class='reason' value='{{reason}}' name="reason" />
Page({
data:{
reason:''
},
bindReason(e) {
this.setData({
reason: e.detail.value
})
}
})
當頁(ye)面表單元素很(hen)多的時(shi)候,更(geng)改值(zhi)就是一(yi)(yi)件體力活了。和小程序一(yi)(yi)比(bi)較,vue的v-model簡直爽的不要不要的。 2.取值vue中,通過(guo)this.reason取值(zhi) 小(xiao)程序中,通過this.data.reason取值(zhi) 七、綁定事件傳參在(zai)vue中,綁定事件傳(chuan)參(can)挺簡單,只需要在(zai)觸發事件的方(fang)法中,把需要傳(chuan)遞的數據作為形參(can)傳(chuan)入就可以(yi)了,例如:
<button @click="say('明天不上班')"></button>
new Vue({
el: '#app',
methods:{
say(arg){
consloe.log(arg)
}
}
})
在小程序中,不能(neng)直接在綁定事件的(de)方(fang)法中傳(chuan)入參數(shu),需(xu)要將(jiang)參數(shu)作為屬(shu)性值,綁定到元素上的(de)data-屬(shu)性上,然后(hou)在方(fang)法中,通過e.currentTarget.dataset.*的(de)方(fang)式(shi)獲取,從而完成參數(shu)的(de)傳(chuan)遞,很麻煩(fan)有沒有...
<view class='tr' bindtap='toApprove' data-id="{{item.id}}"></view>
Page({
data:{
reason:''
},
toApprove(e) {
let id = e.currentTarget.dataset.id;
}
})
八、父子組件通信1.子組件的使用在(zai)vue中,需要:
//子組件 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>
在(zai)小程(cheng)序中,需要: 1.編(bian)寫子組件(jian) 2. 在子組(zu)件的json文件中(zhong),將(jiang)該文件聲明為組(zu)件
{
"component": true
}
3.在需要引(yin)入的父(fu)組件(jian)的json文件(jian)中,在usingComponents填(tian)寫引(yin)入組件(jian)的組件(jian)名以(yi)及路徑
"usingComponents": {
"tab-bar": "../../components/tabBar/tabBar"
}
4.在父組件中,直接引(yin)入即(ji)可 <tab-bar currentpage="index"></tab-bar> 具體代(dai)碼(ma):
// 子組件
<!--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中父組(zu)件(jian)(jian)向(xiang)子(zi)組(zu)件(jian)(jian)傳遞數(shu)據(ju),只需(xu)要(yao)在子(zi)組(zu)件(jian)(jian)通過(guo)v-bind傳入一個值,在子(zi)組(zu)件(jian)(jian)中,通過(guo)props接(jie)收(shou),即可完成數(shu)據(ju)的傳遞,示(shi)例:
// 父組件 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
<template>
<div class="search-box">
<div :title="title" ></div>
</div>
</template>
<script>
export default{
props:{
title:{
type:String,
default:''
}
}
}
</script>
子(zi)組(zu)件和父組(zu)件通信可(ke)以通過this.$emit將方法和數(shu)據(ju)傳(chuan)遞給(gei)父組(zu)件。 在小程序中父(fu)組(zu)件向子組(zu)件通信和vue類(lei)似,但是小(xiao)程序(xu)沒有通過v-bind,而是直接(jie)將值賦值給一(yi)個變量,如下: <tab-bar currentpage="index"></tab-bar> 此處(chu), “index”就是要(yao)向子組件(jian)傳(chuan)遞的值 在子組件(jian)properties中,接收傳遞的值
properties: {
// 彈窗標題
currentpage: { // 屬性名
type: String, // 類型(必填),目前接受的類型包括:String, Number, Boolean, Object, Array, null(表示任意類型)
value: 'index' // 屬性初始值(可選),如果未指定則會根據類型選擇一個
}
}
子組件向父組件通信(xin)和(he)vue也很類似,代碼如下:
//子組件中
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)
}
如(ru)果父(fu)組(zu)件(jian)(jian)想要調用(yong)子組(zu)件(jian)(jian)的(de)方法 vue會給子(zi)組件添加一個ref屬(shu)性,通過this.$refs.ref的(de)值便可以獲取到該子(zi)組件,然(ran)后(hou)便可以調用(yong)子(zi)組件中的(de)任意方(fang)法(fa),例如(ru): //子組件 <bar ref="bar"></bar> //父組件 this.$ref.bar.子組件的方法 小程序是給子組(zu)(zu)件(jian)(jian)添加id或者class,然后通過this.selectComponent找到(dao)子組(zu)(zu)件(jian)(jian),然后再調用子組(zu)(zu)件(jian)(jian)的(de)方法(fa),示例:
//子組件
<bar id="bar"></bar>
// 父組件
this.selectComponent('#id').syaHello()
小程序和vue在這點上太相(xiang)似了,有(you)(you)木有(you)(you)。。。 |