{"record":{"id":"7d07bc11db2b45c4","repo":"vuejs/vue-router","slug":"vue-router-missing-current-instance-method","errorCode":null,"errorMessage":"[vue-router]: Missing current instance. ${method}() must be called inside <script setup> or setup().","messagePattern":"\\[vue-router\\]: Missing current instance\\. (.+?)\\(\\) must be called inside <script setup> or setup\\(\\)\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/composables/utils.js","lineNumber":7,"sourceCode":"import { getCurrentInstance } from 'vue'\n\n// dev only warn if no current instance\n\nexport function throwNoCurrentInstance (method) {\n  if (!getCurrentInstance()) {\n    throw new Error(\n      `[vue-router]: Missing current instance. ${method}() must be called inside <script setup> or setup().`\n    )\n  }\n}\n","sourceCodeStart":1,"sourceCodeEnd":12,"githubUrl":"https://github.com/vuejs/vue-router/blob/680ccc68c506cca9be6207745043245d61fa715a/src/composables/utils.js#L1-L12","documentation":"vue-router's composition APIs (useRouter, useRoute, onBeforeRouteUpdate, onBeforeRouteLeave, useLink) rely on Vue's getCurrentInstance() to find the component instance they belong to. throwNoCurrentInstance throws when none exists, i.e. the API was called outside of a component's setup()/<script setup> context. This is a hard throw (not a warning) so the mistake is caught early in development.","triggerScenarios":"Calling useRouter()/useRoute()/onBeforeRoute*/useLink() at module top level, inside setTimeout/setInterval/Promise.then callbacks, in event handlers after setup finished, in plain .js utility files, or in non-component contexts like store actions.","commonSituations":"Extracting a navigation helper into a standalone composable file and calling useRouter() there directly; calling useRoute() inside a debounce/timeout; upgrading from Vue Router 3 where this.$router was accessible anywhere via the Vue prototype.","solutions":["Move the call into the synchronous body of setup() or <script setup>.","If you need the router outside setup, pass the router instance in explicitly or use the router exported from your router setup module (createRouter result).","Inside async callbacks, capture const router = useRouter() first in setup, then use the captured variable in the callback."],"exampleFix":"// before (utils.js, throws)\nexport function goHome () {\n  const router = useRouter()\n  router.push('/')\n}\n// after (component)\nimport { useRouter } from 'vue-router'\nexport default {\n  setup () {\n    const router = useRouter()\n    const goHome = () => router.push('/')\n    return { goHome }\n  }\n}","handlingStrategy":"validation","validationCode":"import { getCurrentInstance } from 'vue'\nfunction canUseRouterApis () {\n  return Boolean(getCurrentInstance())\n}\n// call synchronously inside setup(); if false, use an explicitly passed router instead","typeGuard":"function hasRouterInstance () {\n  return typeof getCurrentInstance === 'function' && !!getCurrentInstance()\n}","tryCatchPattern":null,"preventionTips":["Only call useRouter/useRoute/onBeforeRoute*/useLink in the synchronous top-level body of setup() or <script setup>.","Capture the router/route objects into local consts in setup before using them in async callbacks or event handlers.","For non-component modules, import the router instance created by createRouter instead of composition APIs.","Lint with eslint-plugin-vue rules / code review to catch router API calls inside setTimeout/Promise callbacks."],"tags":["vue","vue-router","composition-api","lifecycle"],"backgroundTag":"vue-composition-api-outside-setup","analyzedSha":"680ccc68c506cca9be6207745043245d61fa715a","analyzedAt":"2026-09-02T19:19:01.204Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}