1

I have a navbar component, I need to redirect to the login component inside a logout method declared at navbar.

I tried: this.$router.push({name:'MyLogin'});

But I see the error:

vue-router.esm.js?8c4f:1897 TypeError: Cannot read property '$router' of undefined
at eval (main.js?56d7:68)

How can I use router push inside a component?

Or how can I inside this component make access to the router.

EDIT :

At the file: /src/router.js I have the Login route:

export default new Router({
    mode: 'history',
    routes: [
    {
        path: '/login',
        name: 'Login',
        component: LoginView
    },

And at the file: /src/components/Navibar.vue I have the doLogout method:

methods: {
    doLogout(){ 
    this.$router.push({name:'Login'}); 
}

Being new to Vue js I tried this:

doLogout(){
   const router = new router({
    routes: [
              { 
                path: '/login', 
                name: 'Login', 
                component: LoginView 
              }               
        ]
  })
 this.$router.push({name:'Login'}); 

}

I see the error message:

doLogout catch ReferenceError: LoginView is not defined at eval (VM2993 NaviBar.vue:324)

So I tried to add the component:

Vue.component('LoginView', require('Login.vue'));

Which gives me the error:

Module not found: Error: Can't resolve 'Login.vue'

Maybe I am doing the wrong steps?

Which is the correct way to enable this.$router.push() inside one component redirecting to another component?

7
  • 1
    maybe this could help stackoverflow.com/questions/47148363/… Commented Jun 6, 2019 at 20:46
  • 2
    Something is wrong with your component since this is undefined. We can't tell from that one line. What does the rest of the component look like? Commented Jun 6, 2019 at 22:18
  • 1
    Add the whole function over here in which you are doing push in router. Commented Jun 7, 2019 at 6:23
  • The login route is defined this way : Commented Jun 7, 2019 at 12:05
  • export default new Router({ mode: 'history', routes: [ { path: '/login', name: 'Login', component: LoginView }, Commented Jun 7, 2019 at 12:06

1 Answer 1

2

Try declaring as below :

At the file: /src/router.js

const router = new Router({
    mode: 'history',
    routes: [
    {
        path: '/login',
        name: 'Login',
        component: LoginView
    },
export default router

The router variable declared is accessible over all vue components.

Hence at the file: /src/components/Navibar.vue doLogout method :

methods: {
    doLogout(){ 
    this.$router.push({name:'Login'}); 
}
Sign up to request clarification or add additional context in comments.

2 Comments

It will be : const router = new Router({ mode: 'history', routes: [ { path: '/login', name: 'Login', component: LoginView } }) export default router ??
thank´s @Riddhi, now debuging i see the entire router object, this is at the navbar component, just don´t know why it stays on the same page, it does not redirect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.