Skip to content

No this-access in beforeRouteUpdate() when changing route in created() #1286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cprass opened this issue Mar 28, 2017 · 1 comment
Closed

Comments

@cprass
Copy link

cprass commented Mar 28, 2017

Vue.js / vue-router versions
2.2.2 / 2.2.0

I have some subroutes in my app and want to watch for route changes in the parent. I use the beforeRouteUpdate-hook. I have a created-hook in the same Component. In a special state the app uses this.$router.push(...) in the created hook, but that throws an Error.

Vue warn]: Error in created hook: 
(found in <App> at E:\Work\vue\src\App.vue)
warn @ vue.esm.js?65d7:558
handleError @ vue.esm.js?65d7:1443
...
TypeError: Cannot read property 'foo' of undefined
    at beforeRouteUpdate (eval at <anonymous> (app.js:795), <anonymous>:7:21)
    at boundRouteGuard (eval at <anonymous> (app.js:876), <anonymous>:1751:18)

I access this.foo in my beforeRouteUpdate-hook and it works fine but not if there is a redirect in the created hook.

I can have access to this in created(), but not in beforeRouteUpdate(), is this expected?

Here is a repo that reproduces when you open the app and reload the page:
https://github.com/ChrisGitter/vue-router-beforeRouteUpdate/

@cprass cprass changed the title Created-hook route push together with beforeRouteUpdate No this-access in beforeRouteUpdate() when changing route in created() Mar 28, 2017
@posva
Copy link
Member

posva commented Apr 4, 2017

Thanks for reporting it. By triggering the route navigation in the created hook. The beforeRouteUpdate gets called before the router has access to the instance.
A workaround for the moment is using the mounted hook instead of the created one

PS: It's better to show it in a jsfiddle whenever it's possible: http://jsfiddle.net/posva/9r6xhqbp/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants