【发布时间】:2017-07-21 12:16:24
【问题描述】:
这是我的路由代码:
// export so we can use in components
export var router = new VueRouter();
// define routes
router.map({
'home': {
component: Home,
auth: true
},
'login': {
component: Login,
auth: false
}
});
// fallback route
router.redirect({
'*': 'home'
});
router.beforeEach(function (transition) {
console.log("here!");
console.log("beforeeach auth.user.authenticated: "+auth.user.authenticated)
if (transition.to.auth && !auth.user.authenticated) {
// if route requres authentication i.e. auth:true in routes
// and isn't authenticated
transition.redirect('login');
} else {
transition.next();
}
});
// expose the whole thing on element with 'app' as an id
router.start(App, '#app');
这是我的auth/index.js
export default {
user: {
authenticated: false
},
login: function(context, creds, redirect) {
this.user.authenticated=true;
console.log("logged in!");
router.go('/home');
},
logout: function() {
this.user.authenticated=false;
console.log("logout");
router.go('/login');
}
}
我的 Nav.vue:
<template>
<div class="top-nav-bar" v-if="user.authenticated">
// other code here....
<ul class="notification user-drop-down">
<li><a href="#" @click="logout()">Logout</a></li>
</ul>
// other code here ...
</div>
</template>
<script>
import auth from '../services/auth';
export default {
data: function () {
return {
user: auth.user
}
},
methods: {
logout: function () {
auth.logout();
}
}
}
</script>
当我点击注销按钮时,它会重定向到localhost:8080/#!/home
但我的 auth.logout() 有 router.go('/login') 。所以它应该重定向到登录控制器!
当我手动输入浏览器 localhost:8080/!#/home 时,它会正确重定向到 /login 页面。那么为什么注销按钮停留在 /home (我看到一个空白页面并且没有控制台错误!)?
编辑:
我正在使用 vue 1.0.7 和 vue-router 0.7.5
【问题讨论】:
标签: javascript vue.js vue-component vue-router