{"record":{"id":"ec90f39a946fd9bc","repo":"youzan/vant-weapp","slug":"van-notify-selector-context","errorCode":null,"errorMessage":"未找到 van-notify 节点，请确认 selector 及 context 是否正确","messagePattern":"未找到 van-notify 节点，请确认 selector 及 context 是否正确","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"packages/notify/notify.ts","lineNumber":66,"sourceCode":"  return pages[pages.length - 1];\n}\n\nexport default function Notify(options: NotifyOptions | string) {\n  options = { ...currentOptions, ...parseOptions(options) };\n\n  const context = options.context || getContext();\n  const notify = context.selectComponent(options.selector);\n\n  delete options.context;\n  delete options.selector;\n\n  if (notify) {\n    notify.setData(options);\n    notify.show();\n    return notify;\n  }\n\n  console.warn('未找到 van-notify 节点，请确认 selector 及 context 是否正确');\n}\n\nNotify.clear = function (options?: NotifyOptions) {\n  options = { ...defaultOptions, ...parseOptions(options) };\n\n  const context = options.context || getContext();\n  const notify = context.selectComponent(options.selector);\n\n  if (notify) {\n    notify.hide();\n  }\n};\n\nNotify.setDefaultOptions = (options: NotifyOptions) => {\n  Object.assign(currentOptions, options);\n};\n\nNotify.resetDefaultOptions = () => {","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/youzan/vant-weapp/blob/7a7d43757ed19d3ad5e6bca69059e0b9ea565d0b/packages/notify/notify.ts#L48-L84","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nNotify({ context: () => this, message: 'Saved' }); // function context: warn, nothing shown\n// after\nNotify({ context: this, message: 'Saved' });\n// and in page.wxml:\n// <van-notify id=\"van-notify\" />","handlingStrategy":"validation","validationCode":"function assertNotifyNode(context?: object, selector = '#van-notify') {\n  const ctx = context || getCurrentPages()[getCurrentPages().length - 1];\n  if (!ctx || typeof ctx.selectComponent !== 'function' || !ctx.selectComponent(selector)) {\n    throw new Error(\n      `<van-notify ${selector}> not found. Add <van-notify id=\"van-notify\" /> to the page WXML and register it in usingComponents.`\n    );\n  }\n}\n// call before Notify():\n// assertNotifyNode(this, '#van-notify');","typeGuard":"function isNotifyInstance(v: unknown): v is { show(): void; hide(): void; setData(d: object): void } {\n  return (\n    !!v &&\n    typeof (v as any).show === 'function' &&\n    typeof (v as any).hide === 'function'\n  );\n}","tryCatchPattern":"try {\n  const inst = Notify({ message: 'Saved', context: this });\n  if (!isNotifyInstance(inst)) {\n    throw new Error('van-notify node not found; check selector/context');\n  }\n} catch (e) {\n  console.warn('Notify failed:', e);\n  wx.showToast({ title: 'Saved' }); // fallback to native toast\n}","preventionTips":["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 '#'."],"tags":["wechat-miniprogram","vant-weapp","selectcomponent","selector","notify","missing-component-node"],"backgroundTag":"select-component-node-not-found","analyzedSha":"7a7d43757ed19d3ad5e6bca69059e0b9ea565d0b","analyzedAt":"2026-09-02T21:06:17.809Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}