Vue路由器链接仅在刷新后才能工作

时间:2019-07-09 09:04:22

标签: javascript vue.js vue-router

我正在根据当前用户的登录状态更新导航链接。因此,如果用户未登录,则显示注册和登录链接。如果用户已登录,则显示仪表板和注销链接。当我使用登录表单登录时,链接会在导航栏中更改,但是当我单击“注销”链接时,它将不起作用。意味着当我单击注销时,它根本不执行任何操作。甚至在控制台上都没有错误。

现在,如果我刷新页面,然后单击“注销”,那么它将起作用并正确注销用户。

您知道如何解决此问题吗?

下面是代码:

<ul class="navbar-nav ml-auto">

    <router-link tag="li" active-class="active"
        :to="{ name: 'login' }"
        v-if="!this.$store.getters.isAuthenticated">
        <a href="#" class="nav-link">Login</a>
    </router-link>

    <router-link tag="li" active-class="active"
        :to="{ name: 'register' }"
        v-if="!this.$store.getters.isAuthenticated">
        <a href="#" class="nav-link">Register</a>
    </router-link>

    <router-link tag="li" active-class="active"
        :to="{ name: 'dashboard' }"
        v-if="this.$store.getters.isAuthenticated">
        <a href="#" class="nav-link">Dashboard</a>
    </router-link>

    <router-link tag="li" active-class="active"
        :to="{ name: 'logout' }"
        v-if="this.$store.getters.isAuthenticated">
        <a href="#" class="nav-link">Logout</a>
    </router-link>

</ul>

下面是router.js代码

export default new Router({
    mode: 'history',
    routes: [
        {
            path: '/',
            name: 'home',
            component: () => import('./views/Home.vue'),
        },
        {
            path: '/company/login',
            name: 'login',
            component: () => import('./views/Company/Login.vue'),
            meta: {
                forVisitor: true
            }
        },
        {
            path: '/company/register',
            name: 'register',
            component: () => import('./views/Company/Register.vue'),
            meta: {
                forVisitor: true
            }
        },
        {
            path: '/company/logout',
            name: 'logout',
            component: () => import('./views/Company/Logout.vue')
        },
        {
            path: '/dashboard',
            name: 'dashboard',
            component: () => import('./views/Company/Dashboard.vue'),
            meta: {
                forAuth: true
            }
        }
    ]
})

这是main.js文件中的代码

// navigation guard
router.beforeEach(
    (to, from, next) => {
        if(to.matched.some(record => record.meta.forVisitor)){
            if(store.getters.isAuthenticated){
                next({
                    name: "dashboard"
                })
            } else next()
        }
        else if(to.matched.some(record => record.meta.forAuth)){
            if(! store.getters.isAuthenticated){
                next({
                    name: "login"
                })
            } else next()
        }
        else {
            next()
        }
    }
)

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')

1 个答案:

答案 0 :(得分:0)

尝试这个主要问题是tag =“ li”

<ul class="navbar-nav ml-auto">
 <li><router-link active-class="active"
        :to="{ name: 'login' }"
        v-if="!this.$store.getters.isAuthenticated">
        <a href="#" class="nav-link">Login</a>
    </router-link></li>

 <li><router-link  active-class="active"
        :to="{ name: 'register' }"
        v-if="!this.$store.getters.isAuthenticated">
        <a href="#" class="nav-link">Register</a>
    </router-link></li>


    <li><router-link  active-class="active"
        :to="{ name: 'dashboard' }"
        v-if="this.$store.getters.isAuthenticated">
        <a href="#" class="nav-link">Dashboard</a>
    </router-link></li>

   <li> <router-link active-class="active"
        :to="{ name: 'logout' }"
        v-if="this.$store.getters.isAuthenticated">
        <a href="#" class="nav-link">Logout</a>
    </router-link></li>

</ul>

注意:您无需<a href="#" class="nav-link">Logout</a>就可以像

<li> <router-link active-class="active"
            :to="{ name: 'logout' }"
            v-if="this.$store.getters.isAuthenticated">

        </router-link>Logout</li>