question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

history is undefined when the router.replace is called before injecting the router into Vue instance

See original GitHub issue

I need to call router.replace before injecting the router into the Vue instance. But can’t figure out why history property is undefined in that situation:

VueRouter.prototype.replace = function replace (location) {
  this.history.replace(location) // TypeError: Cannot read property 'replace' of undefined
}

Here is the online example (check the console) and the code snippet:

// 0. If using a module system, call Vue.use(VueRouter)

// 1. Define route components.
// These can be imported from other files
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }

// 2. Define some routes
// Each route should map to a component. The "component" can
// either be an actual component constructor created via
// Vue.extend(), or just a component options object.
// We'll talk about nested routes later.
const routes = [
  { path: '/foo', component: Foo },
  { path: '/bar', component: Bar }
]

// 3. Create the router instance and pass the `routes` option
// You can pass in additional options here, but let's
// keep it simple for now.
const router = new VueRouter({
  routes
})

try {
    router.replace('/bar');
} catch(e) {
    console.log(e); // TypeError: Cannot read property 'replace' of undefined
}

// 4. Create and mount the root instance.
// Make sure to inject the router with the router option to make the
// whole app router-aware.
const app = new Vue({
  router
}).$mount('#app')

// Now the app has started!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
LinusBorgcommented, Oct 17, 2016

I didn’t take it that way, don’t worry 😉 I hope you’re successful

1reaction
fnlctrlcommented, Oct 17, 2016

@LinusBorg I didn’t mean you’re wrong, sorry if I made you think that… Your explanation was perfect, and I’m just thinking that it may be an improvement to allow users to do basic replace/push before initializing root vue instance. And I just got a proof-of-concept working that passed all tests, will submit a PR shortly. 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught (in promise) undefined - Vue-router
For example, if you call this.$router.push() in a component and then that component gets destroyed while the route-change is in-progress, ...
Read more >
Migrating from Vue 2
The mode: 'history' option has been replaced with a more flexible one named history . Depending on which mode you were using, you...
Read more >
How To Navigate Between Views with Vue Router
In Vue.js, you can create several views using the first-party library Vue Router. This router makes an association with a view to a...
Read more >
API Reference | Vue Router
Setting replace prop will call router.replace() instead of router.push() when clicked, so the navigation will not leave a history record.
Read more >
Vue Navigation: Use Ionic + Vue Router to Create Multi- ...
In addition, the replace value ensures that the app replaces the current history entry when navigating. note. useIonRouter uses the Vue inject() function...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found