history is undefined when the router.replace is called before injecting the router into Vue instance
See original GitHub issueI 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:
- Created 7 years ago
- Comments:12 (10 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I didn’t take it that way, don’t worry 😉 I hope you’re successful
@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. 😉