uni-app子頁面與父頁面互相傳值
阿新 • • 發佈:2019-03-26
main.js中新增
Vue.prototype.$eventHub = new Vue();
點選列表按鈕開啟新頁面
methods: { click1(e) { let _this = this; //監聽事件 this.$eventHub.$on('fire', function(data) { _this.activeID = data; //清除監聽,不清除會消耗資源 _this.$eventHub.$off('fire'); }); //e.currentTarget.dataset.id 獲取元素上的data-對應的值 uni.navigateTo({ url: '../categoryDetail/categoryDetail?id=' + e.currentTarget.dataset.id }); } }
子頁面獲取傳過來的值
onLoad(opt) {
//opt是傳過來的queryString物件
this.activeID = opt.id;
}
子頁面點選將值傳給父頁面
click1(e) {
//獲取點選物件的值
this.activeID = e.currentTarget.dataset.id;
//先記錄值,下面this指向會不一樣
let aa = this.activeID;
//將資料發射到父級監聽事件中
this.$eventHub.$emit('fire',aa);
//關閉當前視窗
uni.navigateBack({})