des*_*cne 12 javascript vue.js vue-router vuejs2
我有一个使用以下网站的登录问题:
在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()!
}
Run Code Online (Sandbox Code Playgroud)
})
并且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 '/'
}
}
}
Run Code Online (Sandbox Code Playgroud)
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()!
}
})
Run Code Online (Sandbox Code Playgroud)
现在这里this.login只是调用vuex来更新登录用户.
会发生什么,登录后,URL更改为/ home,但DOM不会更新!
成功更改DOM的唯一方法是强制location.reload(),这不是我想要做的,因为它在Head中丢失了我动态加载的G脚本.
关于如何强制视图更新DOM的任何想法?
注意:它仅在用户首次登录时发生,如果他退出并重新登录,重定向就可以了
可能不是一个完美的解决方案,因为它将重新创建组件,但它适用于具有相同路由且需要更新组件的每种情况。
只需更新<router-view/>或<router-view></router-view>与
<router-view :key="$route.fullPath"></router-view>
Run Code Online (Sandbox Code Playgroud)
也许你应该将redirectToAfterLogin函数设置到方法中,这样每次都会重新计算。仅当使用的 v-model 更改时,计算才会被修改。为了坚持函数名称的含义,我将在内部设置路由器推送。
登录.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('/')
}
}
}
Run Code Online (Sandbox Code Playgroud)
“但是,不同之处在于,计算属性是根据其依赖项进行缓存的。计算属性仅在其某些依赖项发生更改时才会重新计算。这意味着只要消息未更改,就可以多次访问reversedMessage计算属性立即返回先前计算的结果,而无需再次运行该函数。”
方法与计算和过滤器:
| 归档时间: |
|
| 查看次数: |
7566 次 |
| 最近记录: |