2

I'm working with the Vue Router and I need to use the router.beforeEachcallback to determine if a certain route has been hit. The only problem with this is that when the callback is used the <router-view></router-view> does not render anything.

app.vue

<template>
    <div id="app">
        <navComponent></navComponent>
        <router-view></router-view>
    </div>
</template>

<script>
    import navComponent from './nav'
        export default {
            components: {
                navComponent
            }
        }
</script>

routes.js

import VueRouter from 'vue-router';

export default new VueRouter({

    routes: [
        {
            path: '/',
            component: require('./views/home')
        },
        {
            path: '/logout'
        }
    ],

    linkActiveClass: 'is-active'
});

app.js

import './bootstrap';
import router from './routes';
import app from './views/app';

router.beforeEach((to, from, next) => {  //if this callback was commented out the <router-view></router-view> would render what i want it to render
    if(to.fullPath === '/logout') {
        axios.get('/logout');
        router.push('/');
    }
});

new Vue({

    el: "#app",

    router,

    render: h => h(app)

});

Sorry in advance if the fix is very simple, I just can't seem to find the solution.

Thanks.

1 Answer 1

7

So it turns out I was missing the next(); call within the beforeEach callback.

import './bootstrap';
import router from './routes';
import app from './views/app';

router.beforeEach((to, from, next) => {
    if(to.fullPath === '/logout') {
        axios.get('/logout');
        router.push('/');
    }

    next(); //this is needed
});
Sign up to request clarification or add additional context in comments.

Comments

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.