Skip to content

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

Closed
@theshem

Description

@theshem

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!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions