From ecd46ba23de45fafebf05a3fba75a3912c668404 Mon Sep 17 00:00:00 2001 From: yutent Date: Mon, 8 May 2023 18:43:04 +0800 Subject: [PATCH] =?UTF-8?q?$emit()=E6=94=AF=E6=8C=81=E7=A6=81=E6=AD=A2?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E5=86=92=E6=B3=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/html.js | 4 +--- src/index.js | 4 ++-- src/utils.js | 10 ++++++++-- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 8333b94..a56c6f0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bd/core", - "version": "1.9.3", + "version": "1.9.4", "type": "module", "description": "百搭UI组件库的核心", "main": "dist/index.js", diff --git a/src/html.js b/src/html.js index 474b47a..ee217a9 100644 --- a/src/html.js +++ b/src/html.js @@ -136,9 +136,7 @@ function getTemplateHtml(strings, type) { let htmlResult = html2 + (strings[len] || '') + (type === SVG_RESULT ? '' : '') if (!Array.isArray(strings) || !strings.hasOwnProperty('raw')) { - let message = 'invalid template strings array' - - throw new Error(message) + throw new Error('invalid html ast') } return [htmlResult, attrNames] } diff --git a/src/index.js b/src/index.js index 8b3ff1e..6b46ca4 100644 --- a/src/index.js +++ b/src/index.js @@ -337,7 +337,7 @@ export class Component extends HTMLElement { unbind(this, type, callback, options) } - $emit(type, data = {}) { - return fire(this, type, data) + $emit(type, data = {}, stop = false) { + return fire(this, type, data, stop) } } diff --git a/src/utils.js b/src/utils.js index 3fbf0d3..b489be7 100644 --- a/src/utils.js +++ b/src/utils.js @@ -185,9 +185,15 @@ export function clearOutsideClick(fn = noop) { unbind(document, 'mousedown', fn) } -export function fire(el, name = 'click', data = {}) { +/** + * @param el 节点 + * @param name 自定义的事件名 + * @param data 要合并进event对象的参数 + * @param stop 是否禁止事件冒泡 + */ +export function fire(el, name = 'click', data = {}, stop) { let ev = document.createEvent('Events') - ev.initEvent(name, true, true) + ev.initEvent(name, !stop, true) Object.assign(ev, data) el.dispatchEvent(ev) }