From 68c5ef7d9fea3f911e250fb5f8b300bc76099e49 Mon Sep 17 00:00:00 2001
From: jiangping <jp@doumee.com>
Date: 星期四, 26 十月 2023 13:55:49 +0800
Subject: [PATCH] 小程序
---
minipro_standard/uni_modules/uview-ui/libs/util/emitter.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 51 insertions(+), 0 deletions(-)
diff --git a/minipro_standard/uni_modules/uview-ui/libs/util/emitter.js b/minipro_standard/uni_modules/uview-ui/libs/util/emitter.js
new file mode 100644
index 0000000..1e64044
--- /dev/null
+++ b/minipro_standard/uni_modules/uview-ui/libs/util/emitter.js
@@ -0,0 +1,51 @@
+/**
+ * 閫掑綊浣跨敤 call 鏂瑰紡this鎸囧悜
+ * @param componentName // 闇�瑕佹壘鐨勭粍浠剁殑鍚嶇О
+ * @param eventName // 浜嬩欢鍚嶇О
+ * @param params // 闇�瑕佷紶閫掔殑鍙傛暟
+ */
+function broadcast(componentName, eventName, params) {
+ // 寰幆瀛愯妭鐐规壘鍒板悕绉颁竴鏍风殑瀛愯妭鐐� 鍚﹀垯 閫掑綊 褰撳墠瀛愯妭鐐�
+ this.$children.map((child) => {
+ if (componentName === child.$options.name) {
+ child.$emit.apply(child, [eventName].concat(params))
+ } else {
+ broadcast.apply(child, [componentName, eventName].concat(params))
+ }
+ })
+}
+export default {
+ methods: {
+ /**
+ * 娲惧彂 (鍚戜笂鏌ユ壘) (涓�涓�)
+ * @param componentName // 闇�瑕佹壘鐨勭粍浠剁殑鍚嶇О
+ * @param eventName // 浜嬩欢鍚嶇О
+ * @param params // 闇�瑕佷紶閫掔殑鍙傛暟
+ */
+ dispatch(componentName, eventName, params) {
+ let parent = this.$parent || this.$root// $parent 鎵惧埌鏈�杩戠殑鐖惰妭鐐� $root 鏍硅妭鐐�
+ let { name } = parent.$options // 鑾峰彇褰撳墠缁勪欢瀹炰緥鐨刵ame
+ // 濡傛灉褰撳墠鏈夎妭鐐� && 褰撳墠娌″悕绉� 涓� 褰撳墠鍚嶇О绛変簬闇�瑕佷紶杩涙潵鐨勫悕绉扮殑鏃跺�欏氨鍘绘煡鎵惧綋鍓嶇殑鑺傜偣
+ // 寰幆鍑哄綋鍓嶅悕绉扮殑涓�鏍风殑缁勪欢瀹炰緥
+ while (parent && (!name || name !== componentName)) {
+ parent = parent.$parent
+ if (parent) {
+ name = parent.$options.name
+ }
+ }
+ // 鏈夎妭鐐硅〃绀哄綋鍓嶆壘鍒颁簡name涓�鏍风殑瀹炰緥
+ if (parent) {
+ parent.$emit.apply(parent, [eventName].concat(params))
+ }
+ },
+ /**
+ * 骞挎挱 (鍚戜笅鏌ユ壘) (骞挎挱澶氫釜)
+ * @param componentName // 闇�瑕佹壘鐨勭粍浠剁殑鍚嶇О
+ * @param eventName // 浜嬩欢鍚嶇О
+ * @param params // 闇�瑕佷紶閫掔殑鍙傛暟
+ */
+ broadcast(componentName, eventName, params) {
+ broadcast.call(this, componentName, eventName, params)
+ }
+ }
+}
--
Gitblit v1.9.3