在实际工作中有很多场景需要在第二个页面中将用户操作之后的将数据回传到上一页面。接下来将我的方案分享给小伙伴。 本次示例采用 uni-app 框架和 weui 样式库 实现思路 创建一个 Emitter,用于事件处理 创建一个 ...
在实际工作中有很多场景需要在第二个页面中将用户操作之后的将数据回传到上一页面。接下来将我的方案分享给小伙伴。
本次示例采用 uni-app 框架和 weui 样式库
实现思路
- 创建一个
Emitter ,用于事件处理 - 创建一个全局的
Storage - 在第一个页面创建一个
emitter 对象,并添加事件监听,将 emitter 存储到 Storage 中 - 在第二个页面从
Storage 中取出 emitter 对象, 并触发事件,将数据传递到第一个页面中做处理
创建 Emitter
function isFunc(fn) {
return typeof fn === 'function'; } export default class Emitter { constructor() { this._store = {}; }
创建 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(); 复制代码
第一个页面中的处理
<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 || '请选择' }}</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();
第二个页面中的处理
<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); // 触发事件并传递数据 this.emitter.emit('onSelect', { ...item }); } } } </script> 复制代码
效果展示
传送门
github
总结
之所以将Storage 定义成全局的,是为了保证第一个页面放到Storage 中和第二个页面从 Storage 中取出的emitter 是同一个实例,如此第一个页面才能正确监听到第二个页面触发的事件。也可以使用 vuex,将 emitter 放到 state 中。
|
转自:http://www.wxapp-union.com/article-5740-1.html