diff --git a/src/App.vue b/src/App.vue index 3dab0a3..a83bee3 100644 --- a/src/App.vue +++ b/src/App.vue @@ -12,9 +12,20 @@ name: 'app', 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') + } + } + }, } diff --git a/src/main.js b/src/main.js index c537e06..0616740 100644 --- a/src/main.js +++ b/src/main.js @@ -86,6 +86,12 @@ const router = new VueRouter({ router.beforeEach((to, from, next) => { 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() }) diff --git a/src/pages/Home.vue b/src/pages/Home.vue index 54f2bce..ad6ae4a 100644 --- a/src/pages/Home.vue +++ b/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: { onSubmit() { console.log('submit!'); @@ -119,6 +119,7 @@ this.$confirm('确认退出吗?', '提示', { //type: 'warning' }).then(() => { + localStorage.removeItem('user'); _this.$router.replace('/login'); }).catch(() => { @@ -126,6 +127,11 @@ } + }, + mounted() { + this.currentPath = this.$route.path; + this.currentPathName = this.$route.name; + this.currentPathNameParent = this.$route.matched[0].name; } }