Browse Source

保存登录状态

master
taylor 8 years ago
parent
commit
3c683709d9
  1. 17
      src/App.vue
  2. 6
      src/main.js
  3. 20
      src/pages/Home.vue

17
src/App.vue

@ -12,9 +12,20 @@
name: 'app', name: 'app',
components: { components: {
}, },
created: function () {
this.$router.replace('/login')
}
// beforeCreate: function () {
// let user = JSON.parse(localStorage.getItem('user'));
// if (!user) {
// this.$router.replace('/login')
// }
// }
watch: {
'$route'(to, from) {//
let user = JSON.parse(localStorage.getItem('user'));
if (!user) {
this.$router.replace('/login')
}
}
},
} }
</script> </script>

6
src/main.js

@ -86,6 +86,12 @@ const router = new VueRouter({
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
NProgress.start(); NProgress.start();
let user = JSON.parse(localStorage.getItem('user'));
console.log(user); console.log(to);
if (!user && to.path != '/login') {
console.log('lgout');
next({ path: '/login' })
}
next() next()
}) })

20
src/pages/Home.vue

@ -94,13 +94,13 @@
} }
} }
}, },
watch: {
'$route'(to, from) {//
this.currentPath = to.path;
this.currentPathName = to.name;
this.currentPathNameParent = to.matched[0].name;
}
},
// watch: {
// '$route'(to, from) {//
// this.currentPath = to.path;
// this.currentPathName = to.name;
// this.currentPathNameParent = to.matched[0].name;
// }
// },
methods: { methods: {
onSubmit() { onSubmit() {
console.log('submit!'); console.log('submit!');
@ -119,6 +119,7 @@
this.$confirm('确认退出吗?', '提示', { this.$confirm('确认退出吗?', '提示', {
//type: 'warning' //type: 'warning'
}).then(() => { }).then(() => {
localStorage.removeItem('user');
_this.$router.replace('/login'); _this.$router.replace('/login');
}).catch(() => { }).catch(() => {
@ -126,6 +127,11 @@
} }
},
mounted() {
this.currentPath = this.$route.path;
this.currentPathName = this.$route.name;
this.currentPathNameParent = this.$route.matched[0].name;
} }
} }
</script> </script>

Loading…
Cancel
Save