diff --git a/packages/dialog/dialog.ts b/packages/dialog/dialog.ts index d88e77f1..371cd305 100644 --- a/packages/dialog/dialog.ts +++ b/packages/dialog/dialog.ts @@ -49,6 +49,8 @@ const Dialog: Dialog = options => { ...options }); queue.push(dialog); + } else { + console.warn('未找到 van-dialog 节点,请确认 selector 及 context 是否正确'); } }); }; diff --git a/packages/notify/README.md b/packages/notify/README.md index 61b47abd..d2e6ec9c 100644 --- a/packages/notify/README.md +++ b/packages/notify/README.md @@ -48,6 +48,7 @@ Notify({ | selector | 自定义选择器 | `String` | `van-notify` | | color | 字体颜色 | `String` | `#fff` | | | backgroundColor | 背景色 | `String` | `#f44` | +| context | 选择器的选择范围,可以传入自定义组件的 this 作为上下文 | `Object` | 当前页面 | ### 更新日志 diff --git a/packages/notify/notify.ts b/packages/notify/notify.ts index 341ae4b6..81872db4 100644 --- a/packages/notify/notify.ts +++ b/packages/notify/notify.ts @@ -3,7 +3,8 @@ import { isObj } from '../common/utils'; type NotifyOptions = { selector?: string; duration?: number; -} + context?: any; +}; const defaultOptions = { selector: '#van-notify', @@ -14,17 +15,22 @@ function parseOptions(text) { return isObj(text) ? text : { text }; } -export default function Notify(options: NotifyOptions = {}) { +function getContext() { const pages = getCurrentPages(); - const ctx = pages[pages.length - 1]; + return pages[pages.length - 1]; +} +export default function Notify(options: NotifyOptions = {}) { options = Object.assign({}, defaultOptions, parseOptions(options)); - const el = ctx.selectComponent(options.selector); + const context = options.context || getContext(); + const notify = context.selectComponent(options.selector); delete options.selector; - if (el) { - el.setData(options); - el.show(); + if (notify) { + notify.setData(options); + notify.show(); + } else { + console.warn('未找到 van-notify 节点,请确认 selector 及 context 是否正确'); } }