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

时间:2016-10-31 18:21:46

标签: javascript vue.js vuejs2 vue-router

我的网站登录问题使用:

  • Vue.js v2.0.3
  • vue-router 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 '/'
            }
        }
    }

router.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来更新登录用户。

登录后,网址更改到/ home,但 DOM不会更新

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

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

注意:只有在用户首次登录时才会发生,如果他退出并重新登录,重定向就可以了

4 个答案:

答案 0 :(得分:2)

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

只需用

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

答案 1 :(得分:1)

Vue尽可能重用组件。您应该使用beforeRouteUpdate对使用相同组件的路由交换机做出反应。

答案 2 :(得分:0)

也许你应该将redirectToAfterLogin函数设置为方法,这样每次都会重新计算。只有在使用v-model更改时才会修改计算结果。为了坚持功能名称的含义,我会将路由器设置在里面。

login.vue:

mounted: function(){
   if (this.auth.isUserLoggedIn){
            // Let's just redirect to the main page
            // this.$router.push(this.redirectToAfterLogins)
            this.redirectToAfterLogins()
   }else{
            Vue.nextTick(() => {
                this.loadGooglePlatform()
            })
   }
},
// computed: {
methods: {
    this.login(userData).then( 
       // () => this.$router.push(this.redirectToAfterLogin), // Login success
       () => this.redirectToAfterLogin(), // Login success
       () => {} // Login failed
    ),
    redirectToAfterLogin: function() {

        if (this.$route.query.redirect){
            // return this.$route.query.redirect
            this.$router.push(this.$route.query.redirect)
        }else{
            // return '/'
            this.$router.push('/')
        }
    }
}

“然而,区别在于计算属性是根据它们的依赖关系进行缓存的。计算属性只会在其某些依赖项发生更改时重新计算。这意味着只要消息没有更改,就可以多次访问reversedMessage computed属性将立即返回先前计算的结果,而不必再次运行该函数。“

方法与计算和过滤器:

答案 3 :(得分:0)

我有同样的问题“ URL更改为/ home,但是DOM无法更新”。
在我的项目中,标签“ transition”引起了问题。
希望对您有所帮助!

相关问题