youzan/vant-weapp · warning
未找到 van-notify 节点,请确认 selector 及 context 是否正确
Error message
未找到 van-notify 节点,请确认 selector 及 context 是否正确
What it means
This warning is emitted by Vant Weapp's Notify() when context.selectComponent(options.selector) cannot find a <van-notify> component instance. The imperative Notify API only configures and shows an existing <van-notify id="van-notify"> node declared in the page's WXML; if the node is absent, or the selector/context doesn't resolve to the page containing it, the library warns and returns undefined without showing any notification.
Source
Thrown at packages/notify/notify.ts:66
return pages[pages.length - 1];
}
export default function Notify(options: NotifyOptions | string) {
options = { ...currentOptions, ...parseOptions(options) };
const context = options.context || getContext();
const notify = context.selectComponent(options.selector);
delete options.context;
delete options.selector;
if (notify) {
notify.setData(options);
notify.show();
return notify;
}
console.warn('未找到 van-notify 节点,请确认 selector 及 context 是否正确');
}
Notify.clear = function (options?: NotifyOptions) {
options = { ...defaultOptions, ...parseOptions(options) };
const context = options.context || getContext();
const notify = context.selectComponent(options.selector);
if (notify) {
notify.hide();
}
};
Notify.setDefaultOptions = (options: NotifyOptions) => {
Object.assign(currentOptions, options);
};
Notify.resetDefaultOptions = () => {View on GitHub (pinned to 7a7d43757e)
Solutions
- Add <van-notify id="van-notify" /> to the page's WXML and register "van-notify": "@vant/weapp/notify/index" in the page's JSON usingComponents.
- If you used a custom selector, pass it in the options: Notify({ selector: '#top-notify', message: '...' }) and ensure the node has that exact id.
- If the node lives in a child component, pass options.context as the instance (object) that contains it, e.g. Notify({ context: this.selectComponent('#child'), message: '...' }) — resolve any function context yourself, since Notify does not support function-typed context.
- Don't call Notify before a page exists (e.g. in App.onLaunch); move the call into a page/component lifecycle like onReady.
- Check the return value: Notify returns the component instance on success and undefined on this failure path, so you can detect and log the misconfiguration.
Example fix
// before
Notify({ context: () => this, message: 'Saved' }); // function context: warn, nothing shown
// after
Notify({ context: this, message: 'Saved' });
// and in page.wxml:
// <van-notify id="van-notify" /> Defensive patterns
Strategy: validation
Validate before calling
function assertNotifyNode(context?: object, selector = '#van-notify') {
const ctx = context || getCurrentPages()[getCurrentPages().length - 1];
if (!ctx || typeof ctx.selectComponent !== 'function' || !ctx.selectComponent(selector)) {
throw new Error(
`<van-notify ${selector}> not found. Add <van-notify id="van-notify" /> to the page WXML and register it in usingComponents.`
);
}
}
// call before Notify():
// assertNotifyNode(this, '#van-notify'); Type guard
function isNotifyInstance(v: unknown): v is { show(): void; hide(): void; setData(d: object): void } {
return (
!!v &&
typeof (v as any).show === 'function' &&
typeof (v as any).hide === 'function'
);
} Try / catch
try {
const inst = Notify({ message: 'Saved', context: this });
if (!isNotifyInstance(inst)) {
throw new Error('van-notify node not found; check selector/context');
}
} catch (e) {
console.warn('Notify failed:', e);
wx.showToast({ title: 'Saved' }); // fallback to native toast
} Prevention
- Add <van-notify id="van-notify" /> to every page that calls Notify(); unlike Toast usage, Notify requires the node per page.
- Remember Notify's options.context accepts only an instance, not a function — resolve () => this.selectComponent(...) yourself before passing.
- Don't call Notify from App.onLaunch or other pre-page code where getCurrentPages() is empty; call it from page lifecycle handlers instead.
- Check Notify's return value; undefined means the node wasn't found and no notification was shown.
- Keep custom ids and options.selector in sync, including the leading '#'.
When it happens
Trigger: Calling Notify('some message') or Notify(options) when (1) the page WXML has no <van-notify id="van-notify" />, (2) options.selector is customized (e.g. '#top-notify') but no node with that id exists, (3) options.context is a different page/component instance than the one containing the node — note options.context is used directly here (no function form like Toast/Dialog support), so passing a function yields an invalid selectComponent context, or (4) getCurrentPages() is empty (Notify called too early, e.g. in app-level code before any page exists), making getContext() return undefined.
Common situations: Adding Notify() calls in app.js or a util module executed before the first page renders; using Notify on a page whose JSON forgot to register the van-notify component (node silently not rendered); declaring <van-notify> inside a custom component but calling Notify() from the page without context; migrating from Toast to Notify and forgetting the extra <van-notify> node, since Notify requires it per page; passing a function as context (supported in Toast/Dialog options but not here), which makes selectComponent fail.
Related errors
AI-assisted analysis of youzan/vant-weapp@7a7d43757e (2026-09-02).
Data as JSON: /api/errors/ec90f39a946fd9bc.
Report an issue: GitHub.