Vue-router 2 更改路由但不更新视图?

新手上路,请多包涵

我在使用以下网站时遇到登录问题:

  • Vue.js v2.0.3
  • vue-路由器 v2.0.1
  • vuex v0.8.2

routes.js 我有一个简单的拦截器设置

router.beforeEach((to, from, next) => {
if (to.matched.some(record => record.meta.requiresAuth)) {
    // this route requires auth, check if logged in
    // if not, redirect to login page.
    if (!router.app.auth.isUserLoggedIn) {
        next({
            path: '/login',
            query: { redirect: to.fullPath }
        })
    } else {
        next()
    }
} else {
    next() // make sure to always call next()!
}

})

login.vue 中,它在仅使用 Google API 登录成功后处理登录页面逻辑,我称之为:

 this.login(userData).then(
    () => this.$router.push(this.redirectToAfterLogin), // Login success
    () => {} // Login failed
)

mounted: function(){
if (this.auth.isUserLoggedIn){
            // Let's just redirect to the main page
            this.$router.push(this.redirectToAfterLogins)
        }else{
            Vue.nextTick(() => {
                this.loadGooglePlatform()
            })}}

computed: {
        redirectToAfterLogin: function() {
            if (this.$route.query.redirect){
                return this.$route.query.redirect
            }else{
                return '/'
            }
        }
    }

路由器.js

 var VueRouter = require('vue-router')

// Router setup
export const router = new VueRouter({
    linkActiveClass: "is-active",
    mode: 'history',
    saveScrollPosition: true,
    routes: [
        { path: '', name: 'root', redirect: '/home' },
        { path: '/login', name: 'login', meta: { loadingNotRequired: true }, component: require('./pages/login.vue') },
        { path: '/logout', name: 'logout', meta: { loadingNotRequired: true }, component: require('./pages/logout.vue') },
        { path: '/home', name: 'home', title: 'Home', redirect: '/home/random', component: require('./pages/home.vue'),
            children: [
                { path: 'random', name: 'random', meta: { requiresAuth: true }, title: 'Random', component: require('./pages/random.vue') }
            ]
        }
    ]
})

// Redirect to login page if not logged In
router.beforeEach((to, from, next) => {
    if (to.matched.some(record => record.meta.requiresAuth)) {
        // this route requires auth, check if logged in
        // if not, redirect to login page.
        if (!router.app.auth.isUserLoggedIn) {
            next({
                path: '/login',
                query: { redirect: to.fullPath }
            })
        } else {
            next()
        }
    } else {
        next() // make sure to always call next()!
    }
})

现在这里 this.login 只是对 vuex 的调用,用于更新登录用户。

发生的事情是登录后, URL 更改 为 /home,但 DOM 没有更新

成功更改 DOM 的唯一方法是强制 location.reload() 这不是我想要做的,因为它在 Head 中丢失了我动态加载的 G 脚本。

关于如何强制视图更新 DOM 的任何想法?

注意:它只发生在用户第一次登录时,如果他注销并重新登录,重定向就可以了

原文由 desicne 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 619
1 个回答

可能不是一个完美的解决方案,因为它将重新创建组件,但它适用于具有相同路由且需要更新组件的每种情况。

只需更新 <router-view/><router-view></router-view>

 <router-view :key="$route.fullPath"></router-view>

原文由 Abhay 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题