微信小程序跨頁(yè)面數(shù)據(jù)傳遞事件響應(yīng)實(shí)現(xiàn)過程解析
這篇文章主要介紹了微信小程序跨頁(yè)面數(shù)據(jù)傳遞事件響應(yīng)實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
在實(shí)際工作中有很多場(chǎng)景需要在第二個(gè)頁(yè)面中將用戶操作之后的將數(shù)據(jù)回傳到上一頁(yè)面。接下來將我的方案分享給小伙伴。 本次示例采用 uni-app 框架和 weui 樣式庫(kù) 實(shí)現(xiàn)思路 創(chuàng)建一個(gè) Emitter,用于事件處理 創(chuàng)建一個(gè) ...
在實(shí)際工作中有很多場(chǎng)景需要在第二個(gè)頁(yè)面中將用戶操作之后的將數(shù)據(jù)回傳到上一頁(yè)面。接下來將我的方案分享給小伙伴。
本次示例采用 uni-app 框架和 weui 樣式庫(kù)
實(shí)現(xiàn)思路
- 創(chuàng)建一個(gè) Emitter,用于事件處理
- 創(chuàng)建一個(gè)全局的 Storage
- 在第一個(gè)頁(yè)面創(chuàng)建一個(gè) emitter 對(duì)象,并添加事件監(jiān)聽,將 emitter 存儲(chǔ)到 Storage 中
- 在第二個(gè)頁(yè)面從 Storage 中取出 emitter 對(duì)象, 并觸發(fā)事件,將數(shù)據(jù)傳遞到第一個(gè)頁(yè)面中做處理
創(chuàng)建 Emitter
function isFunc(fn) {
return typeof fn === 'function';
}
export default class Emitter {
constructor() {
this._store = {};
}
/**
* 事件監(jiān)聽
* @param {String} event 事件名
* @param {Function} listener 事件回調(diào)函數(shù)
*/
on(event, listener) {
const listeners = this._store[event] || (this._store[event] = []);
listeners.push(listener);
}
/**
* 取消事件監(jiān)聽
* @param {String} event 事件名
* @param {Function} listener 事件回調(diào)函數(shù)
*/
off(event, listener) {
const listeners = this._store[event] || (this._store[event] = []);
listeners.splice(listeners.findIndex(item => item === listener), 1);
}
/**
* 事件監(jiān)聽 僅監(jiān)聽一次
* @param {String} event 事件名
* @param {Function} listener 事件回調(diào)函數(shù)
*/
once(event, listener) {
const proxyListener = (data) => {
isFunc(listener) && listener.call(null, data);
this.off(event, proxyListener);
}
this.on(event, proxyListener);
}
/**
* 觸發(fā)事件
* @param {String} 事件名
* @param {Object} 傳給事件回調(diào)函數(shù)的參數(shù)
*/
emit(event, data) {
const listeners = this._store[event] || (this._store[event] = []);
for (const listener of listeners) {
isFunc(listener) && listener.call(null, data);
}
}
}
創(chuàng)建 Storage
export class Storage {
constructor() {
this._store = {};
}
add(key, val) {
this._store[key] = val;
}
get(key) {
return this._store[key];
}
remove(key) {
delete this._store[key];
}
clear() {
this._store = {};
}
}
export default new Storage();
第一個(gè)頁(yè)面中的處理
<template>
<div class="page">
<div class="weui-cells__title">選擇城市</div>
<div class="weui-cells weui-cells_after-title">
<navigator :url="`../select/select?id=${cityId}`" class="weui-cell weui-cell_access" hover-class="weui-cell_active">
<div class="weui-cell__hd weui-label">所在城市</div>
<div class="weui-cell__bd" :style="{color: cityName || '#999'}">{{ cityName || '請(qǐng)選擇' }}</div>
<div class="weui-cell__ft weui-cell__ft_in-access"></div>
</navigator>
</div>
</div>
</template>
<script>
import Emitter from '../../utils/emitter';
import storage from '../../utils/storage';
export default {
data() {
return {
cityId: '',
cityName: '',
}
},
onLoad() {
const emitter = new Emitter();
// 將emitter存到storage中
storage.add('indexEmitter', emitter);
// 添加事件監(jiān)聽
emitter.on('onSelect', this.handleSelect);
},
methods: {
// 事件處理
handleSelect(data) {
this.cityId = data.id;
this.cityName = data.text;
}
}
}
</script>
第二個(gè)頁(yè)面中的處理
<template>
<div class="page">
<div class="weui-cells__title">城市列表</div>
<div class="weui-cells weui-cells_after-title">
<radio-group @change="handleChange">
<label class="weui-cell weui-check__label" v-for="item in list" :key="item.id">
<radio class="weui-check" :value="item.id" :checked="`${item.id}` === selectedId" />
<div class="weui-cell__bd">{{ item.text }}</div>
<div v-if="`${item.id}` === selectedId" class="weui-cell__ft weui-cell__ft_in-radio">
<icon class="weui-icon-radio" type="success_no_circle" size="16" />
</div>
</label>
</radio-group>
</div>
</div>
</template>
<script>
import storage from '../../utils/storage';
export default {
data() {
return {
list: [
{ id: 0, text: '北京' },
{ id: 1, text: '上海' },
{ id: 2, text: '廣州' },
{ id: 3, text: '深圳' },
{ id: 4, text: '杭州' },
],
selectedId: ''
}
},
onLoad({ id }) {
this.selectedId = id;
// 取出 emitter
this.emitter = storage.get('indexEmitter');
},
methods: {
handleChange(e) {
this.selectedId = e.detail.value;
const item = this.list.find(({ id }) => `${id}` === e.detail.value);
// 觸發(fā)事件并傳遞數(shù)據(jù)
this.emitter.emit('onSelect', { ...item });
}
}
}
</script>
傳送門
總結(jié)
之所以將Storage定義成全局的,是為了保證第一個(gè)頁(yè)面放到Storage中和第二個(gè)頁(yè)面從 Storage 中取出的emitter是同一個(gè)實(shí)例,如此第一個(gè)頁(yè)面才能正確監(jiān)聽到第二個(gè)頁(yè)面觸發(fā)的事件。也可以使用 vuex,將 emitter 放到 state 中。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序 頁(yè)面跳轉(zhuǎn)和數(shù)據(jù)傳遞實(shí)例詳解
- 微信小程序 頁(yè)面跳轉(zhuǎn)及數(shù)據(jù)傳遞詳解
- 微信小程序 動(dòng)態(tài)修改頁(yè)面數(shù)據(jù)及參數(shù)傳遞過程詳解
- 微信小程序?qū)W習(xí)筆記之跳轉(zhuǎn)頁(yè)面、傳遞參數(shù)獲得數(shù)據(jù)操作圖文詳解
- 微信小程序開發(fā)之?dāng)?shù)據(jù)存儲(chǔ) 參數(shù)傳遞 數(shù)據(jù)緩存
- 微信小程序 跳轉(zhuǎn)傳遞數(shù)據(jù)的實(shí)例
- 微信小程序開發(fā)實(shí)用技巧之?dāng)?shù)據(jù)傳遞和存儲(chǔ)
相關(guān)文章
javascript實(shí)現(xiàn)動(dòng)態(tài)表頭及表列的展現(xiàn)方法
這篇文章主要介紹了javascript實(shí)現(xiàn)動(dòng)態(tài)表頭及表列的展現(xiàn)方法,涉及javascript動(dòng)態(tài)操作table元素的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
TypeScript遍歷Array的方法(for,forEach,every)
本文主要介紹了TypeScript遍歷Array的方法(for,forEach,every),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
JS實(shí)現(xiàn)很實(shí)用的對(duì)聯(lián)廣告代碼(可自適應(yīng)高度)
這篇文章主要介紹了JS實(shí)現(xiàn)很實(shí)用的對(duì)聯(lián)廣告代碼,可實(shí)現(xiàn)固定相對(duì)位置懸浮展示及跟隨屏幕上下滑動(dòng)等功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
JS實(shí)現(xiàn)Date日期格式的本地化的方法小結(jié)
為了更好的更新多語(yǔ)言日期的顯示,所以希望實(shí)現(xiàn)日期的本地化格式顯示要求,常規(guī)的特殊字符型格式化無法滿足顯示要求,這里整理了幾種我思考實(shí)現(xiàn)的本地化實(shí)現(xiàn)功能2024-06-06
js+數(shù)組實(shí)現(xiàn)網(wǎng)頁(yè)上顯示時(shí)間/星期幾的實(shí)用方法
在網(wǎng)頁(yè)上顯示時(shí)間(年月日/時(shí)分秒),很多新手朋友都想實(shí)現(xiàn)這樣的功能,本文整理了一些實(shí)用技巧,殺出來與大家分享,感興趣的朋友可以了解下2013-01-01
js點(diǎn)擊列表文字對(duì)應(yīng)該行顯示背景顏色的實(shí)現(xiàn)代碼
這篇文章主要介紹了js點(diǎn)擊列表文字對(duì)應(yīng)該行顯示背景顏色的實(shí)現(xiàn)代碼,感興趣的小伙伴可以參考下2015-08-08

