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.

this.app won't work inside beforeEnter

See original GitHub issue

Inside a beforeEnter per-route guard the Router instance is this.a instead of this.

Example:

new VueRouter({
    routes: [
        {
            beforeEnter: (to, from, next) => {
                this.app // app is undefined
                this.a.app // this works!
            }
        }
    ]
})

It’s normal?

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
Naskalincommented, Aug 15, 2018

same problem

// vue-router@3.0.1
// vue@2.5.16
new VueRouter({
    routes: [
        {
            name: 'test', path: '/test', component: Test,
            beforeEnter: (to, from, next) => {
                console.log(this); // {a: VueRouter}
                console.log(this.a.app); // Vue
            },
            beforeEnter(to, from, next) {
                console.log(this); // undefined
            },
            // and for completeness
            beforeEnter: function (to, from, next) {
                console.log(this); // undefined
            },
        }
    ],
});
1reaction
fnlctrlcommented, Oct 8, 2016

You’re using arrow functions that doesn’t have access to the desired this, please use normal functions instead:

beforeEnter(to, from, next) {
    this.app
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue-router: beforeEnter guard doesn't work properly for ...
The best solution I found was to use beforeEach guard instead of beforeEnter. beforeEnter is a per route guard, and then it was...
Read more >
[vue-router] beforeEnter access vue app's methods - Get Help
I want to call the method of vue app from router. I do this like: var router = New VueRouter({ routes: [{ path:...
Read more >
How to implement route guard in vue.js | by Vipin Cheriyanveetil
These guards are hooked to our function named “guardMyroute”. The guard's name is “beforeEnter”. So what happens is any time this route is...
Read more >
Routes | Framework7 Documentation
Routes defined on app init are default routes, they will be available for any View/Router in the app. If you have a multi-view/router...
Read more >
Using Vue 3's Router in Practice - Daily.dev
So I have modified the App.vue file and routes file to configure a new ... to add another <router-view> inside the Message view...
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