uni-app實(shí)現(xiàn)頁面通信EventChannel的操作方法
uni-app實(shí)現(xiàn)頁面通信EventChannel
之前使用了EventBus的方法實(shí)現(xiàn)不同頁面組件之間的一個(gè)通信,在uni-app中,我們也可以使用uni-app API —— uni.navigateTo來實(shí)現(xiàn)頁面間的通信。注:2.8.9+ 支持頁面間事件通信通道。
1. 向被打開頁面?zhèn)魉蛿?shù)據(jù)
// index.vue
<script setup>
uni.navigateTo({
url: '/pages/tender/detail', // 跳轉(zhuǎn)詳情頁面
success:function(res){
// 通過eventChannel向被打開頁面?zhèn)魉蛿?shù)據(jù)
res.eventChannel.emit('toDetailEmits', { data: 'index to detail' })
}
});
</script>// detail.vue
import { onLoad } from '@dcloudio/uni-app';
import { ref, getCurrentInstance} from 'vue';
const instance = getCurrentInstance().proxy
<script setup>
onLoad(()=>{
const eventChannel = instance.getOpenerEventChannel();
eventChannel.on('toDetailEmits',(data)=>{
console.log(data,'data') // 輸出結(jié)果如下
})
})
</script>
2. 如果需要獲取被打開頁面?zhèn)魉偷疆?dāng)前頁面的數(shù)據(jù)
// index.vue
<script setup>
uni.navigateTo({
url: '/pages/tender/detail', // 跳轉(zhuǎn)詳情頁面
events:{
// 為指定事件添加一個(gè)監(jiān)聽器,獲取被打開頁面?zhèn)魉偷疆?dāng)前頁面的數(shù)據(jù)
updataEmits:function(data){
console.log(data,'data index') // 輸出結(jié)果如下
// 可以在當(dāng)前頁做一些操作....
}
},
success:function(res){
// 通過eventChannel向被打開頁面?zhèn)魉蛿?shù)據(jù)
res.eventChannel.emit('toDetailEmits', { data: 'index to detail' })
}
});
</script>// detail.vue
import { onLoad } from '@dcloudio/uni-app';
import { ref, getCurrentInstance} from 'vue';
const instance = getCurrentInstance().proxy
<script setup>
// 如點(diǎn)擊某一按鈕
const cancle = () => {
const eventChannel = instance.getOpenerEventChannel();
eventChannel.emit('updataEmits',{data:'detail to index'})
uni.navigateBack()
}
onLoad(()=>{
const eventChannel = instance.getOpenerEventChannel();
eventChannel.on('toDetailEmits',(data)=>{
console.log(data,'data')
})
})
</script>
到此這篇關(guān)于uni-app實(shí)現(xiàn)頁面通信EventChannel的操作方法的文章就介紹到這了,更多相關(guān)uni-app頁面通信EventChannel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
three.js中正交與透視投影相機(jī)的實(shí)戰(zhàn)應(yīng)用指南
在three.js中攝像機(jī)的作用就是不斷的拍攝我們創(chuàng)建好的場(chǎng)景,然后通過渲染器渲染到屏幕中,下面這篇文章主要給大家介紹了關(guān)于three.js中正交與透視投影相機(jī)應(yīng)用的相關(guān)資料,需要的朋友可以參考下2022-08-08
javascript attachEvent和addEventListener使用方法
attachEvent與addEventListener區(qū)別 適應(yīng)的瀏覽器版本不同,同時(shí)在使用的過程中要注意2009-03-03
基于JavaScript實(shí)現(xiàn)簡(jiǎn)單的輪播圖
這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)簡(jiǎn)單的輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03
ES6知識(shí)點(diǎn)整理之對(duì)象解構(gòu)賦值應(yīng)用示例
這篇文章主要介紹了ES6知識(shí)點(diǎn)整理之對(duì)象解構(gòu)賦值應(yīng)用,結(jié)合實(shí)例形式分析了ES6對(duì)象解構(gòu)賦值相關(guān)概念、原理、出現(xiàn)的問題及相應(yīng)解決方法,需要的朋友可以參考下2019-04-04
JavaScript寫的一個(gè)DIV 彈出網(wǎng)頁對(duì)話框
自己整理得一個(gè)JavaScript寫的一個(gè)DIV 彈出網(wǎng)頁對(duì)話框2009-08-08
微信小程序后端實(shí)現(xiàn)授權(quán)登錄
這篇文章主要介紹了微信小程序后端實(shí)現(xiàn)授權(quán)登錄,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
JavaScript設(shè)計(jì)模式組合設(shè)計(jì)模式案例
這篇文章主要介紹了JavaScript設(shè)計(jì)模式組合設(shè)計(jì)模式案例,組合設(shè)計(jì)模式是用于將多個(gè)部分通過組合的方式行成一個(gè)整體,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下2022-06-06

