From d1448cb0ef10f358bb7bddb4e1ec268515e0b787 Mon Sep 17 00:00:00 2001 From: gaoluyang <2820782392@qq.com> Date: 星期二, 15 七月 2025 11:46:57 +0800 Subject: [PATCH] 项目初始化 --- uni_modules/uview-ui/libs/util/emitter.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 51 insertions(+), 0 deletions(-) diff --git a/uni_modules/uview-ui/libs/util/emitter.js b/uni_modules/uview-ui/libs/util/emitter.js new file mode 100644 index 0000000..1e64044 --- /dev/null +++ b/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