16

我的网站存在登录问题,该网站使用:

  • 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 的任何想法?

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

4

4 回答 4

10

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

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

<router-view :key="$route.fullPath"></router-view>
于 2019-01-06T13:21:48.360 回答
2

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

于 2019-01-08T15:13:41.343 回答
1

我有同样的问题“URL 更改为 /home,但 DOM 没有更新”。
在我的项目中,标签“过渡”造成了问题。
希望对你有帮助!</p>

于 2019-01-18T02:50:34.443 回答
0

也许您应该将 redirectToAfterLogin 函数设置为方法,这样每次都会重新计算。仅当使用的 v-model 更改时,才会修改计算值。为了坚持函数名的含义,我将路由器 push 设置在里面。

登录.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 计算属性的多次访问就会立即返回先前计算的结果,而无需再次运行该函数。”

方法与计算和过滤器:

于 2018-06-13T08:54:00.790 回答