自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

vue和微信小程序的區(qū)別、比較

開發(fā) 前端 移動開發(fā)
寫了vue項(xiàng)目和小程序,發(fā)現(xiàn)二者有許多相同之處,在此想總結(jié)一下二者的共同點(diǎn)和區(qū)別。相比之下,小程序的鉤子函數(shù)要簡單得多。

寫了vue項(xiàng)目和小程序,發(fā)現(xiàn)二者有許多相同之處,在此想總結(jié)一下二者的共同點(diǎn)和區(qū)別。

一、生命周期

先貼兩張圖:

vue生命周期

小程序生命周期

相比之下,小程序的鉤子函數(shù)要簡單得多。

vue的鉤子函數(shù)在跳轉(zhuǎn)新頁面時(shí),鉤子函數(shù)都會觸發(fā),但是小程序的鉤子函數(shù),頁面不同的跳轉(zhuǎn)方式,觸發(fā)的鉤子并不一樣。

  • onLoad: 頁面加載

一個頁面只會調(diào)用一次,可以在 onLoad 中獲取打開當(dāng)前頁面所調(diào)用的 query 參數(shù)。

  • onShow: 頁面顯示

每次打開頁面都會調(diào)用一次。

  • onReady: 頁面初次渲染完成

一個頁面只會調(diào)用一次,代表頁面已經(jīng)準(zhǔn)備妥當(dāng),可以和視圖層進(jìn)行交互。

對界面的設(shè)置如wx.setNavigationBarTitle請?jiān)趏nReady之后設(shè)置。詳見生命周期

  • onHide: 頁面隱藏

當(dāng)navigateTo或底部tab切換時(shí)調(diào)用。

  • onUnload: 頁面卸載

當(dāng)redirectTo或navigateBack的時(shí)候調(diào)用。

數(shù)據(jù)請求

在頁面加載請求數(shù)據(jù)時(shí),兩者鉤子的使用有些類似,vue一般會在created或者mounted中請求數(shù)據(jù),而在小程序,會在onLoad或者onShow中請求數(shù)據(jù)。

二、數(shù)據(jù)綁定

VUE:vue動態(tài)綁定一個變量的值為元素的某個屬性的時(shí)候,會在變量前面加上冒號:,例: 

  1. <img :src="imgSrc"/> 

小程序:綁定某個變量的值為元素屬性時(shí),會用兩個大括號括起來,如果不加括號,為被認(rèn)為是字符串。例:

  1. <image src="{{imgSrc}}"></image> 

三、列表渲染

直接貼代碼,兩者還是有些相似

vue: 

  1. <ul id="example-1" 
  2.   <li v-for="item in items" 
  3.     {{ item.message }}  
  4.   </li>  
  5. </ul>  
  6.  
  7. var example1 = new Vue({  
  8.   el: '#example-1' 
  9.   data: {  
  10.     items: [  
  11.       { message: 'Foo' },  
  12.       { message: 'Bar' }  
  13.     ] 
  14.    }  
  15. })  

小程序: 

  1. Page({  
  2.   data: {  
  3.     items: [  
  4.       { message: 'Foo' },  
  5.       { message: 'Bar' }  
  6.     ]  
  7.   }  
  8. })  
  9.  
  10. <text wx:for="{{items}}">{{item}}</text>  

四、顯示與隱藏元素

vue中,使用v-if 和v-show控制元素的顯示和隱藏

小程序中,使用wx-if和hidden控制元素的顯示和隱藏

五、事件處理

vue:使用v-on:event綁定事件,或者使用@event綁定事件,例如: 

  1. <button v-on:click="counter += 1">Add 1</button>  
  2. <button v-on:click.stop="counter+=1">Add1</button>  //阻止事件冒泡 

 

小程序中,全用bindtap(bind+event),或者catchtap(catch+event)綁定事件,例如: 

  1. <button bindtap="noWork">明天不上班</button>  
  2. <button catchtap="noWork">明天不上班</button>  //阻止事件冒泡  

六、數(shù)據(jù)雙向綁定

1.設(shè)置值

在vue中,只需要再表單元素上加上v-model,然后再綁定data中對應(yīng)的一個值,當(dāng)表單元素內(nèi)容發(fā)生變化時(shí),data中對應(yīng)的值也會相應(yīng)改變,這是vue非常nice的一點(diǎn)。 

  1. <div id="app" 
  2.     <input v-model="reason" placeholder="填寫理由" class='reason'/>  
  3. </div>  
  4.  
  5. new Vue({  
  6.   el: '#app' 
  7.   data: {  
  8.    reason:''  
  9.   }  
  10. })  

但是在小程序中,卻沒有這個功能。那怎么辦呢?

當(dāng)表單內(nèi)容發(fā)生變化時(shí),會觸發(fā)表單元素上綁定的方法,然后在該方法中,通過this.setData({key:value})來將表單上的值賦值給data中的對應(yīng)值。

下面是代碼,可以感受一下: 

  1. <input bindinput="bindReason" placeholder="填寫理由" class='reason' value='{{reason}}' name="reason" />  
  2. Page({  
  3. data:{  
  4.     reason:''  
  5. },  
  6. bindReason(e) {  
  7.     this.setData({  
  8.       reason: e.detail.value  
  9.     })  
  10.   }  
  11. }) 

當(dāng)頁面表單元素很多的時(shí)候,更改值就是一件體力活了。和小程序一比較,vue的v-model簡直爽的不要不要的。

2.取值

vue中,通過this.reason取值

小程序中,通過this.data.reason取值

七、綁定事件傳參

在vue中,綁定事件傳參挺簡單,只需要在觸發(fā)事件的方法中,把需要傳遞的數(shù)據(jù)作為形參傳入就可以了,例如: 

  1. <button @click="say('明天不上班')"></button>  
  2. new Vue({  
  3.   el: '#app' 
  4.   methods:{  
  5.     say(arg){  
  6.     consloe.log(arg)  
  7.     }  
  8.   }  
  9. })  

在小程序中,不能直接在綁定事件的方法中傳入?yún)?shù),需要將參數(shù)作為屬性值,綁定到元素上的data-屬性上,然后在方法中,通過e.currentTarget.dataset.*的方式獲取,從而完成參數(shù)的傳遞,很麻煩有沒有... 

  1. <view class='tr' bindtap='toApprove' data-id="{{item.id}}"></view 
  2. Page({  
  3. data:{  
  4.     reason:''  
  5. },  
  6. toApprove(e) {  
  7.     let id = e.currentTarget.dataset.id;  
  8.   }  
  9. })  

八、父子組件通信

1.子組件的使用

在vue中,需要:

  1. 編寫子組件
  2. 在需要使用的父組件中通過import引入
  3. 在vue的components中注冊
  4. 在模板中使用 
  1. //子組件 bar.vue  
  2. <template>  
  3.   <div class="search-box" 
  4.     <div @click="say" :title="title" class="icon-dismiss"></div>  
  5.   </div>  
  6. </template>  
  7. <script>  
  8. export default 
  9. props:{  
  10.     title:{  
  11.        type:String,  
  12.        default:''  
  13.       }  
  14.     }  
  15. }, 
  16.  
  17. methods:{  
  18.     say(){ 
  19.         console.log('明天不上班');  
  20.        this.$emit('helloWorld' 
  21.     }  
  22. </script>  
  23.  
  24. // 父組件 foo.vue  
  25. <template>  
  26.   <div class="container" 
  27.     <bar :title="title" @helloWorld="helloWorld"></bar>  
  28.   </div>  
  29. </template>  
  30.  
  31. <script>  
  32. import Bar from './bar.vue'  
  33. export default 
  34. data:{  
  35.     title:"我是標(biāo)題"  
  36. },  
  37. methods:{  
  38.     helloWorld(){  
  39.         console.log('我接收到子組件傳遞的事件了' 
  40.     }  
  41. },  
  42. components:{ 
  43.      Bar  
  44.  
  45. </script>  

在小程序中,需要:

    1.編寫子組件

    2. 在子組件的json文件中,將該文件聲明為組件 

  1.  
  2.   "component"true  
  3.  

    3.在需要引入的父組件的json文件中,在usingComponents填寫引入組件的組件名以及路徑 

  1. "usingComponents": {  
  2.     "tab-bar""../../components/tabBar/tabBar"  
  3.  

     4.在父組件中,直接引入即可 

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

        具體代碼: 

  1. // 子組件  
  2. <!--components/tabBar/tabBar.wxml-->  
  3. <view class='tabbar-wrapper' 
  4.   <view class='left-bar {{currentpage==="index"?"active":""}}' bindtap='jumpToIndex' 
  5.     <text class='iconfont icon-shouye'></text>  
  6.     <view>首頁</view 
  7.   </view 
  8.   <view class='right-bar {{currentpage==="setting"?"active":""}}' bindtap='jumpToSetting' 
  9.     <text class='iconfont icon-shezhi'></text>  
  10.     <view>設(shè)置</view 
  11.   </view 
  12. </view 

2.父子組件間通信

在vue中

父組件向子組件傳遞數(shù)據(jù),只需要在子組件通過v-bind傳入一個值,在子組件中,通過props接收,即可完成數(shù)據(jù)的傳遞,示例: 

  1. // 父組件 foo.vue  
  2. <template>  
  3.   <div class="container" 
  4.     <bar :title="title"></bar>  
  5.   </div>  
  6. </template>  
  7. <script>  
  8. import Bar from './bar.vue'  
  9. export default 
  10. data:{  
  11.     title:"我是標(biāo)題"  
  12. },  
  13. components:{  
  14.     Bar  
  15.  
  16. </script>  
  17.  
  18. // 子組件bar.vue  
  19. <template>  
  20.   <div class="search-box" 
  21.     <div :title="title" ></div>  
  22.   </div>  
  23. </template>  
  24. <script>  
  25. export default 
  26. props:{  
  27.     title:{  
  28.        type:String,  
  29.        default:''  
  30.       }  
  31.     }  
  32.  
  33. </script>  

子組件和父組件通信可以通過this.$emit將方法和數(shù)據(jù)傳遞給父組件。

在小程序中

父組件向子組件通信和vue類似,但是小程序沒有通過v-bind,而是直接將值賦值給一個變量,如下: 

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

此處, “index”就是要向子組件傳遞的值

在子組件properties中,接收傳遞的值 

  1. properties: {  
  2.     // 彈窗標(biāo)題  
  3.     currentpage: {            // 屬性名  
  4.       type: String,     // 類型(必填),目前接受的類型包括:String, Number, Boolean, Object, Array, null(表示任意類型)  
  5.       value: 'index'     // 屬性初始值(可選),如果未指定則會根據(jù)類型選擇一個 
  6.     } 
  7.  

子組件向父組件通信和vue也很類似,代碼如下: 

  1. //子組件中  
  2. methods: {     
  3.     // 傳遞給父組件  
  4.     cancelBut: function (e) {  
  5.       var that = this;  
  6.       var myEventDetail = { pickerShow: false, type: 'cancel' } // detail對象,提供給事件監(jiān)聽函數(shù)  
  7.       this.triggerEvent('myevent', myEventDetail) //myevent自定義名稱事件,父組件中使用  
  8.     }, 
  9.  
  10. //父組件中  
  11. <bar bind:myevent="toggleToast"></bar>   
  12. // 獲取子組件信息  
  13. toggleToast(e){  
  14.     console.log(e.detail)  
  15.  

如果父組件想要調(diào)用子組件的方法

vue會給子組件添加一個ref屬性,通過this.$refs.ref的值便可以獲取到該子組件,然后便可以調(diào)用子組件中的任意方法,例如: 

  1. //子組件  
  2. <bar ref="bar"></bar>  
  3. //父組件  
  4. this.$ref.bar.子組件的方法  

小程序是給子組件添加id或者class,然后通過this.selectComponent找到子組件,然后再調(diào)用子組件的方法,示例: 

  1. //子組件  
  2. <bar id="bar"></bar>  
  3. // 父組件  
  4. this.selectComponent('#id').syaHello()  

小程序和vue在這點(diǎn)上太相似了,有木有。。。

九、廢話

還有好多地方?jīng)]寫,之后再慢慢加上、精簡。感覺自己寫的有點(diǎn)冗余,大佬勿噴?。?!

如果覺得有幫助,希望幫忙點(diǎn)個贊和收藏

責(zé)任編輯:龐桂玉 來源: segmentfault
相關(guān)推薦

2017-05-08 15:03:07

微信小程序開發(fā)實(shí)戰(zhàn)

2017-02-06 13:32:12

微信小程序思想

2016-10-20 21:02:12

微信小程序javascript

2017-01-09 10:01:49

微信小程序

2017-06-09 10:06:54

微信小程序架構(gòu)分析

2017-06-09 12:58:20

微信小程序架構(gòu)分析

2017-06-09 10:40:00

微信小程序架構(gòu)分析

2016-09-28 18:10:59

微信程序MINA

2016-11-04 10:31:49

微信程序指南

2018-05-31 23:36:30

vue小程序App

2016-11-22 11:23:52

微信小程序騰訊微信

2016-09-27 16:38:24

JavaScript微信Web

2016-09-27 15:40:58

微信程序前端

2016-11-04 10:49:48

微信小程序

2021-06-10 10:51:27

程序基礎(chǔ)架構(gòu)

2016-11-19 18:06:44

微信小程序張小龍

2021-03-03 14:23:06

微信小程序互聯(lián)網(wǎng)

2016-11-04 10:30:17

微信小程序

2021-03-05 06:25:19

fiddler微信公眾號小程序

2023-03-26 00:00:00

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號