一、页面通讯手段?
vue中:
1.页面跳转传值
2.本地缓存
localStorage
3.vuex
4.祖先传给子孙 provide inject
5.父组件传子组件 props
6.子组件传父组件 $emit触发自定义事件
小程序:
1.页面跳转传参
2.globalData
3.缓存传值
wx.setStorage
4.vuex
uniapp:
1.路由传参(简单传参,参数比较少)
A页面向B页面传递参数
uni.navigateTo({
url: 'test/test?id=1&name=张三'
});
B页面接收A页面传递的参数
export default {
onLoad: function (option) { //option为object类型,会序列化上个页面传递的参数
console.log(option.id); //打印出上个页面传递的参数。
console.log(option.name); //打印出上个页面传递的参数。
}
}
2.globalData
以下是 App.vue 中定义globalData的相关配置:
<script>
export default {
globalData: {
text: 'text'
}
}
</script>
js中操作globalData的方式如下: getApp().globalData.text = 'test'
3.本地缓存
uni.setStorage({
key: 'storage_key',
data: 'hello',
success: function () {
console.log('success');
}
});
4.vuex
5.uni自带api
触发全局的自定义事件。附加参数都会传给监听器回调。
uni.$emit('update',{msg:'页面更新'})
监听全局的自定义事件。事件可以由 uni.$emit 触发,回调函数会接收所有传入事件触发函数的额外参数。
uni.$on('update',function(data){
console.log('监听到事件来自 update ,携带参数 msg 为:' + data.msg);
})
二、页面跳转方法?
vue中:
1.组件 router-link
2.编程式导航 this.$router.push()
小程序:
1.组件navigator open-type
2.api
wx.navigateTo(OBJECT)
wx.redirectTo(OBJECT)
wx.reLaunch(OBJECT)
wx.switchTab(OBJECT)
wx.navigateBack(OBJECT)
uniapp:
1.组件navigator open-type
2.api
uni.navigateTo(OBJECT)
uni.redirectTo(OBJECT)
uni.reLaunch(OBJECT)
uni.switchTab(OBJECT)
uni.navigateBack(OBJECT)