{"record":{"id":"51e1a1e758bac5ee","repo":"youzan/vant-weapp","slug":"van-toast-selector-context","errorCode":null,"errorMessage":"未找到 van-toast 节点，请确认 selector 及 context 是否正确","messagePattern":"未找到 van-toast 节点，请确认 selector 及 context 是否正确","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"packages/toast/toast.ts","lineNumber":61,"sourceCode":"function getContext() {\n  const pages = getCurrentPages();\n  return pages[pages.length - 1];\n}\n\nfunction Toast(toastOptions: ToastOptions | ToastMessage) {\n  const options = {\n    ...currentOptions,\n    ...parseOptions(toastOptions),\n  } as ToastOptions;\n\n  const context =\n    (typeof options.context === 'function'\n      ? options.context()\n      : options.context) || getContext();\n  const toast = context.selectComponent(options.selector as string);\n\n  if (!toast) {\n    console.warn('未找到 van-toast 节点，请确认 selector 及 context 是否正确');\n    return;\n  }\n\n  delete options.context;\n  delete options.selector;\n\n  toast.clear = () => {\n    toast.setData({ show: false });\n\n    if (options.onClose) {\n      options.onClose();\n    }\n  };\n\n  queue.push(toast);\n  toast.setData(options);\n  clearTimeout(toast.timer);\n","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/youzan/vant-weapp/blob/7a7d43757ed19d3ad5e6bca69059e0b9ea565d0b/packages/toast/toast.ts#L43-L79","documentation":"This warning is emitted by Vant Weapp's Toast() (also via the createMethod shortcuts Toast.loading/Toast.success/Toast.fail, which call the same function) when context.selectComponent(options.selector) cannot find a <van-toast> component instance. The imperative Toast API only configures and shows an existing <van-toast id=\"van-toast\"> node declared in WXML; if it can't be resolved, the library warns and returns undefined instead of showing the toast, and any returned-instance chaining fails.","triggerScenarios":"Calling Toast('msg') or Toast.loading/success/fail when (1) the page WXML lacks <van-toast id=\"van-toast\" />, (2) options.selector is set to a custom id that doesn't exist in the resolved context, (3) options.context (instance or () => instance) resolves to a page/component that doesn't directly contain the <van-toast> node, or (4) the call happens while getCurrentPages() is empty or before the page tree is available, so the fallback getContext() yields nothing useful.","commonSituations":"Calling Toast from a shared utility/service module while the toast node was added to only some pages; forgetting to add <van-toast id=\"van-toast\" /> to a newly created page that imports the imperative API; declaring the node inside a custom component and calling Toast.success from the page without passing context: () => this.selectComponent('#child'); giving the node a different id (e.g. id=\"toast\") while keeping the default selector '#van-toast'; calling Toast during onLaunch/App-level code before any page exists.","solutions":["Add <van-toast id=\"van-toast\" /> to the WXML of every page (or the containing component) that uses the imperative Toast API, and register the component in the page JSON.","If the node is in a child component, pass options.context: () => this.selectComponent('#child') (or the instance) so selectComponent searches the right subtree.","If you renamed the node, pass the matching selector, e.g. Toast.success({ selector: '#my-toast', message: 'ok' }).","Call Toast only from page/component code after the page has rendered (e.g. onReady or later event handlers), not from App.onLaunch.","Guard on the return value: Toast returns the instance on success and undefined here — check it before calling instance-based APIs like toast.clear()."],"exampleFix":"// before\nToast.loading({ message: 'Loading...', context: this.selectComponent('#child') }); // node lives in child but selectComponent called on child's own scope may miss/wrong scope\n// after\nToast.loading({ message: 'Loading...', context: () => this.selectComponent('#child') });\n// and page.wxml (or child wxml) contains:\n// <van-toast id=\"van-toast\" />","handlingStrategy":"validation","validationCode":"function assertToastNode(context?: (() => object) | object, selector = '#van-toast') {\n  const ctx =\n    (typeof context === 'function' ? context() : context) ||\n    getCurrentPages()[getCurrentPages().length - 1];\n  if (!ctx || typeof ctx.selectComponent !== 'function' || !ctx.selectComponent(selector)) {\n    throw new Error(\n      `<van-toast ${selector}> not found. Add <van-toast id=\"van-toast\" /> to the page WXML and register it in usingComponents.`\n    );\n  }\n}\n// call before Toast()/Toast.loading etc:\n// assertToastNode(() => this.selectComponent('#child'));","typeGuard":"function isToastInstance(v: unknown): v is { clear(): void; setData(d: object): void; timer?: number } {\n  return !!v && typeof (v as any).clear === 'function';\n}","tryCatchPattern":"try {\n  const toast = Toast.loading({ message: 'Loading...', context: () => this.selectComponent('#child') });\n  if (!isToastInstance(toast)) {\n    throw new Error('van-toast node not found; check selector/context');\n  }\n} catch (e) {\n  console.warn('Toast failed:', e);\n  wx.showLoading({ title: 'Loading' }); // native fallback\n}","preventionTips":["Add <van-toast id=\"van-toast\" /> to every page WXML that uses the imperative Toast API (Toast, Toast.loading/success/fail); maintain a page template that includes it.","If the node lives in a child component, always pass context: () => this.selectComponent('#child') so selectComponent searches the correct subtree.","Match custom ids to options.selector exactly, including the leading '#'.","Only call Toast after a page exists and has rendered (event handlers, onReady+), never in App.onLaunch.","Check the return value before using instance methods like toast.clear(); undefined means the node was missing."],"tags":["wechat-miniprogram","vant-weapp","selectcomponent","selector","toast","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"}