From 3d3362e4300d49db23de48575d26c8d501fdbfd8 Mon Sep 17 00:00:00 2001 From: Tevin <tingquanren@163.com> Date: Fri, 20 Aug 2021 18:52:00 +0800 Subject: [PATCH] 优化跨端通讯机制,实现从全局接收App通知,并通知页面 --- bases/Pilot.js | 29 +++++++++++++++++++++++++++++ 1 files changed, 29 insertions(+), 0 deletions(-) diff --git a/bases/Pilot.js b/bases/Pilot.js index babc9de..d7d3966 100644 --- a/bases/Pilot.js +++ b/bases/Pilot.js @@ -3,6 +3,7 @@ * @author Tevin */ +import Taro, { getCurrentInstance } from '@tarojs/taro'; import { Fetcher } from './Fetcher'; import { Tools } from '@components/common/Tools'; import project from '@project'; @@ -52,9 +53,37 @@ options.methods[name] = this[name]; } }); + this._bindTaroPage(options); return options; } + _bindTaroPage(options) { + // 非App内嵌模式,不执行 + if (!project.appHybrid) { + return; + } + // 绑定页面实例到Page + const { onLoad, onUnload, onBridge } = options; + const option2 = { + onLoad() { + const instance = getCurrentInstance(); + instance.page.$component = this; + onLoad && onLoad.call(this); + // 绑定 onBridge 到页面实例 + this.$onBridge = onBridge || (() => { + }); + }, + onUnload() { + const instance = getCurrentInstance(); + delete instance.page.$component; + onUnload && onUnload.call(this); + }, + }; + options.onLoad = option2.onLoad; + options.onUnload = option2.onUnload; + delete options.onBridge; + } + /** * 转换静态图片引用 * @param assets -- Gitblit v1.9.1