From 3c683709d98a23644307646ab9b9a0bd488fb08a Mon Sep 17 00:00:00 2001 From: taylor <709161610@qq.com> Date: Fri, 20 Jan 2017 12:35:43 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=9D=E5=AD=98=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.vue | 17 ++++++++++++++--- src/main.js | 6 ++++++ src/pages/Home.vue | 20 +++++++++++++------- 3 files changed, 33 insertions(+), 10 deletions(-) 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; } } From f2cdbdeb3706387a3b980391828fe80a36b703f8 Mon Sep 17 00:00:00 2001 From: taylor <709161610@qq.com> Date: Sun, 22 Jan 2017 21:30:10 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=9D=E5=AD=98=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.vue | 2 +- src/main.js | 4 +--- src/mockdata/user.js | 2 +- src/pages/Home.vue | 27 ++++++++++++++++++--------- src/pages/Login.vue | 8 ++------ 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/App.vue b/src/App.vue index a83bee3..b90d312 100644 --- a/src/App.vue +++ b/src/App.vue @@ -20,7 +20,7 @@ // } watch: { '$route'(to, from) {//监听路由改变 - let user = JSON.parse(localStorage.getItem('user')); + let user = JSON.parse(sessionStorage.getItem('user')); if (!user) { this.$router.replace('/login') } diff --git a/src/main.js b/src/main.js index 0616740..ba5fbd7 100644 --- a/src/main.js +++ b/src/main.js @@ -86,10 +86,8 @@ const router = new VueRouter({ router.beforeEach((to, from, next) => { NProgress.start(); - let user = JSON.parse(localStorage.getItem('user')); - console.log(user); console.log(to); + let user = JSON.parse(sessionStorage.getItem('user')); if (!user && to.path != '/login') { - console.log('lgout'); next({ path: '/login' }) } next() diff --git a/src/mockdata/user.js b/src/mockdata/user.js index 6ef85fb..f9c511d 100644 --- a/src/mockdata/user.js +++ b/src/mockdata/user.js @@ -4,7 +4,7 @@ const LoginUsers = [ id: 1, username: 'admin', password: '123456', - avatar: '', + avatar: 'https://raw.githubusercontent.com/taylorchen709/markdown-images/master/vueadmin/user.png', name: '张某某' } ]; diff --git a/src/pages/Home.vue b/src/pages/Home.vue index ad6ae4a..d161826 100644 --- a/src/pages/Home.vue +++ b/src/pages/Home.vue @@ -7,7 +7,7 @@ - 张某某 + {{sysUserName}} 我的消息 @@ -82,6 +82,8 @@ currentPath: '/table', currentPathName: 'Table', currentPathNameParent: '导航一', + sysUserName: '', + sysUserAvatar: '', form: { name: '', region: '', @@ -94,13 +96,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,7 +121,7 @@ this.$confirm('确认退出吗?', '提示', { //type: 'warning' }).then(() => { - localStorage.removeItem('user'); + sessionStorage.removeItem('user'); _this.$router.replace('/login'); }).catch(() => { @@ -132,6 +134,13 @@ this.currentPath = this.$route.path; this.currentPathName = this.$route.name; this.currentPathNameParent = this.$route.matched[0].name; + + var user = sessionStorage.getItem('user'); + if (user) { + user = JSON.parse(user); + this.sysUserName = user.name || ''; + this.sysUserAvatar = user.avatar || ''; + } } } diff --git a/src/pages/Login.vue b/src/pages/Login.vue index 8ade801..2e288ed 100644 --- a/src/pages/Login.vue +++ b/src/pages/Login.vue @@ -62,12 +62,8 @@ type: 'error' }); } else { - localStorage.setItem('user', JSON.stringify(user)); - if (this.$route.query.redirect) { - this.$router.push({ path: this.$route.query.redirect }); - } else { - this.$router.push({ path: '/table' }); - } + sessionStorage.setItem('user', JSON.stringify(user)); + this.$router.push({ path: '/table' }); } }); } else {