写了vue项目和小程序,发现两者有许多相同之处,在此想总结一下两者的共同点和区别。前端
1、生命周期vue
先贴两张图: vue生命周期json
小程序生命周期小程序
相比之下,小程序的钩子函数要简单得多。 vue的钩子函数在跳转新页面时,钩子函数都会触发,可是小程序的钩子函数,页面不一样的跳转方式,触发的钩子并不同。 onLoad:页面加载 一个页面只会调用一次,能够在 onLoad 中获取打开当前页面所调用的 query 参数。 onShow:页面显示 每次打开页面都会调用一次。 onReady:页面初次渲染完成 一个页面只会调用一次,表明页面已经准备稳当,能够和视图层进行交互。 对界面的设置如 wx.setNavigationBarTitle请在 onReady以后设置。详见生命周期。 onHide:页面隐藏 当 navigateTo或底部tab切换时调用。 onUnload:页面卸载 当 redirectTo或 navigateBack的时候调用。 数据请求 在页面加载请求数据时,二者钩子的使用有些相似,vue通常会在 created或者 mounted中请求数据,而在小程序,会在 onLoad或者 onShow中请求数据。app
2、数据绑定ide
vue:vue动态绑定一个变量的值为元素的某个属性的时候,会在变量前面加上冒号:,例:函数
<img :src="imgSrc"/>
小程序:绑定某个变量的值为元素属性时,会用两个大括号括起来,若是不加括号,为被认为是字符串。例:this
<image src="{{imgSrc}}"></image>
3、列表渲染双向绑定
直接贴代码,二者仍是有些类似: vue:code
<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>
4、显示与隐藏元素
vue中,使用 v-if 和 v-show控制元素的显示和隐藏。 小程序中,使用 wx-if和 hidden控制元素的显示和隐藏。
5、事件处理
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> //阻止事件冒泡
6、数据双向绑定
一、设置值
在vue中,只须要再表单元素上加上 v-model,而后再绑定 data中对应的一个值,当表单元素内容发生变化时, data中对应的值也会相应改变,这是vue很是nice的一点。
<div id="app"> <input v-model="reason" placeholder="填写理由" class='reason'/> </div> new Vue({ el: '#app', data: { reason:'' } })
可是在小程序中,却没有这个功能。那怎么办呢? 当表单内容发生变化时,会触发表单元素上绑定的方法,而后在该方法中,经过 this.setData({key:value})来将表单上的值赋值给 data中的对应值。 下面是代码,能够感觉一下:
<input bindinput="bindReason" placeholder="填写理由" class='reason' value='{{reason}}' name="reason" /> Page({ data:{ reason:'' }, bindReason(e) { this.setData({ reason: e.detail.value }) } })
当页面表单元素不少的时候,更改值就是一件体力活了。和小程序一比较,vue的 v-model简直爽的不要不要的。
二、取值
vue中,经过 this.reason取值。 小程序中,经过 this.data.reason取值。
7、绑定事件传参
在vue中,绑定事件传参挺简单,只须要在触发事件的方法中,把须要传递的数据做为形参传入就能够了,例如:
<button @click="say('明天不上班')"></button> new Vue({ el: '#app', methods:{ say(arg){ consloe.log(arg) } } })
在 小程序中,不能直接在绑定事件的方法中传入参数,须要将参数做为属性值,绑定到元素上的 data-属性上,而后在方法中,经过 e.currentTarget.dataset.*的方式获取,从而完成参数的传递,很麻烦有没有...
<view class='tr' bindtap='toApprove' data-id="{{item.id}}"></view> Page({//在此我向你们推荐一个前端全栈开发交流圈:619586920 突破技术瓶颈,提高思惟能力 data:{ reason:'' }, toApprove(e) { let id = e.currentTarget.dataset.id; } })
8、父子组件通讯
一、子组件的使用 在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:{ 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>//在此我向你们推荐一个前端全栈开发交流圈:619586920 突破技术瓶颈,提高思惟能力 <view class='right-bar {{currentpage==="setting"?"active":""}}' bindtap='jumpToSetting'> <text class='iconfont icon-shezhi'></text> <view>设置</view> </view> </view>
二、父子组件间通讯
在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> //在此我向你们推荐一个前端全栈开发交流圈:619586920 突破技术瓶颈,提高思惟能力 // 子组件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' // 属性初始值(可选),若是未指定则会根据类型选择一个 } }
子组件向父组件通讯和 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) }
若是父组件想要调用子组件的方法 vue会给子组件添加一个 ref属性,经过 this.$refs.ref的值即可以获取到该子组件,而后即可以调用子组件中的任意方法,例如:
//子组件 <bar ref="bar"></bar> //在此我向你们推荐一个前端全栈开发交流圈:619586920 突破技术瓶颈,提高思惟能力 //父组件 this.$ref.bar.子组件的方法
小程序是给子组件添加 id或者 class,而后经过 this.selectComponent找到子组件,而后再调用子组件的方法,示例:
//子组件 <bar id="bar"></bar> // 父组件 this.selectComponent('#id').syaHello()
小程序和vue在这点上很类似
结语
感谢您的观看,若有不足之处,欢迎批评指正。