From f26f29d84e0a68831a6af14dab3eec5500496d2e Mon Sep 17 00:00:00 2001 From: spring <2396852758@qq.com> Date: 星期三, 28 五月 2025 16:48:52 +0800 Subject: [PATCH] 初始化项目 --- uview-ui/libs/util/emitter.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 51 insertions(+), 0 deletions(-) diff --git a/uview-ui/libs/util/emitter.js b/uview-ui/libs/util/emitter.js new file mode 100644 index 0000000..228016e --- /dev/null +++ b/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.name; // 鑾峰彇褰撳墠缁勪欢瀹炰緥鐨刵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