【问题标题】:Vue router-view not rending when using router.beforeEachVue router-view 在使用 router.beforeEach 时不呈现
【发布时间】:2017-05-30 17:28:12
【问题描述】:

我正在使用 Vue 路由器,我需要使用 router.beforeEachcallback 来确定某个路由是否已被击中。唯一的问题是,当使用回调时,<router-view></router-view> 不会呈现任何内容。

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)

});

如果修复很简单,请提前抱歉,我似乎找不到解决方案。

谢谢。

【问题讨论】:

    标签: vue.js vue-component vue-router


    【解决方案1】:

    原来我在beforeEach 回调中错过了next(); 调用。

    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
    });
    

    【讨论】:

    • 救了我的培根!
    猜你喜欢
    • 2019-08-18
    • 2021-05-26
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 2022-11-09
    相关资源
    最近更新 更多