{"record":{"id":"1abda8023ab6b220","repo":"youzan/vant-weapp","slug":"van-dialog-selector-context","errorCode":null,"errorMessage":"未找到 van-dialog 节点，请确认 selector 及 context 是否正确","messagePattern":"未找到 van-dialog 节点，请确认 selector 及 context 是否正确","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"packages/dialog/dialog.ts","lineNumber":113,"sourceCode":"\n      if (dialog) {\n        dialog.setData({\n          callback: (\n            action: Action,\n            instance: WechatMiniprogram.Component.TrivialInstance\n          ) => {\n            action === 'confirm' ? resolve(instance) : reject(instance);\n          },\n          ...options,\n        });\n\n        wx.nextTick(() => {\n          dialog.setData({ show: true });\n        });\n\n        queue.push(dialog);\n      } else {\n        console.warn(\n          '未找到 van-dialog 节点，请确认 selector 及 context 是否正确'\n        );\n      }\n    }\n  );\n};\n\nDialog.alert = (options: DialogOptions) => Dialog(options);\n\nDialog.confirm = (options: DialogOptions) =>\n  Dialog({\n    showCancelButton: true,\n    ...options,\n  });\n\nDialog.close = () => {\n  queue.forEach((dialog) => {\n    dialog.close();","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/youzan/vant-weapp/blob/7a7d43757ed19d3ad5e6bca69059e0b9ea565d0b/packages/dialog/dialog.ts#L95-L131","documentation":"This warning is emitted by Vant Weapp's Dialog.alert/Dialog.confirm when context.selectComponent(options.selector) fails to find a <van-dialog> component instance in the current page/component tree. The imperative Dialog API does not create the component for you — it only drives an existing <van-dialog id=\"van-dialog\"> node declared in WXML — so when the node is missing (or the selector/context is wrong), the library can only warn; no dialog is shown and the returned Promise never resolves or rejects, silently hanging any await on it.","triggerScenarios":"Calling Dialog.alert(options) or Dialog.confirm(options) while (1) the page WXML lacks <van-dialog id=\"van-dialog\" />, (2) options.selector points to an id that doesn't exist (e.g. selector: '#my-dialog' with no matching id), (3) options.context points to a page/component instance that does not contain the <van-dialog> node (e.g. dialog declared inside a child component but no context passed, or a function context returning the wrong instance), or (4) Dialog is called before the page tree is attached (e.g. in onLoad before initial render completes on some platforms).","commonSituations":"Forgetting to add <van-dialog id=\"van-dialog\" /> to the page that calls Dialog.alert; placing the dialog node inside a custom component but calling Dialog from the page without passing context: () => this.selectComponent('#the-child'); using with-function contexts (share pages, custom-tab-bar pages) where getCurrentPages()'s top page isn't the one declaring the node; copy-pasting the default selector '#van-dialog' while giving the node a different id; abstract/factory pages generated dynamically where WXML wasn't updated after adding the API call.","solutions":["Add <van-dialog id=\"van-dialog\" /> to the WXML of the page (or component, with matching context) that calls Dialog.alert/Dialog.confirm, and register the component in its JSON (\"van-dialog\": \"@vant/weapp/dialog/index\").","If you renamed the node, pass the matching selector, e.g. Dialog.alert({ selector: '#my-dialog', ... }).","If the <van-dialog> node lives in a child component, pass options.context pointing at the instance that directly contains it, e.g. Dialog.alert({ context: () => this.selectComponent('#child') }) or context: this when inside a component.","Defer the call until the page is rendered (wx.nextTick or after first render) if you call it during onLoad/onAttach before selectComponent can resolve.","Wrap awaits in a timeout/race guard, since on this path the Promise never settles and awaiting code hangs silently."],"exampleFix":"// before: page.ts\nonLoad() {\n  Dialog.confirm({ message: 'Delete?' }); // warns, promise never settles\n}\n// after: page.wxml adds the node\n<van-dialog id=\"van-dialog\" show=\"{{ false }}\" />\n// page.ts\nonReady() {\n  Dialog.confirm({ message: 'Delete?' });\n}","handlingStrategy":"validation","validationCode":"function assertDialogNode(context?: object, selector = '#van-dialog') {\n  const ctx = context || getCurrentPages()[getCurrentPages().length - 1];\n  if (!ctx || !ctx.selectComponent || !ctx.selectComponent(selector)) {\n    throw new Error(\n      `<van-dialog ${selector}> not found in target context. Add <van-dialog id=\"van-dialog\" /> to the page WXML and register it in usingComponents.`\n    );\n  }\n}\n// call before Dialog.alert/confirm:\n// assertDialogNode(this, '#van-dialog');","typeGuard":"function hasSelectComponent(\n  ctx: unknown\n): ctx is { selectComponent: (s: string) => WechatMiniprogram.Component.TrivialInstance | null } {\n  return (\n    !!ctx &&\n    typeof (ctx as any).selectComponent === 'function' &&\n    !!(ctx as any).selectComponent('#van-dialog')\n  );\n}","tryCatchPattern":"try {\n  await Promise.race([\n    Dialog.confirm({ message: 'Delete?' }),\n    new Promise((_, rej) => setTimeout(() => rej(new Error('dialog node missing / never resolved')), 5000)),\n  ]);\n} catch (e) {\n  console.warn('Dialog failed or node missing:', e);\n}","preventionTips":["Always declare <van-dialog id=\"van-dialog\" /> in every page WXML that uses the imperative Dialog API; keep a project snippet/checklist for it.","If using a custom selector, make the id and options.selector match exactly (case-sensitive, include the leading '#').","When the node is inside a child component, pass options.context referencing the instance that directly contains it, and resolve function contexts yourself.","Never rely on Dialog()'s promise settling on the failure path — race it with a timeout when awaiting.","After upgrading Vant Weapp, re-verify component registration names in usingComponents ('@vant/weapp/dialog/index')."],"tags":["wechat-miniprogram","vant-weapp","selectcomponent","selector","dialog","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"}