File tree Expand file tree Collapse file tree 5 files changed +37
-11
lines changed Expand file tree Collapse file tree 5 files changed +37
-11
lines changed Original file line number Diff line number Diff line change 1212 name: ' app' ,
1313 components: {
1414 },
15- created : function () {
16- this .$router .replace (' /login' )
17- }
15+ // beforeCreate: function () {
16+ // let user = JSON.parse(localStorage.getItem('user'));
17+ // if (!user) {
18+ // this.$router.replace('/login')
19+ // }
20+ // }
21+ watch: {
22+ ' $route' (to, from) {// 监听路由改变
23+ let user = JSON .parse (sessionStorage .getItem (' user' ));
24+ if (! user) {
25+ this .$router .replace (' /login' )
26+ }
27+ }
28+ },
1829 }
1930 </script >
2031
Original file line number Diff line number Diff line change @@ -86,6 +86,10 @@ const router = new VueRouter({
8686
8787router . beforeEach ( ( to , from , next ) => {
8888 NProgress . start ( ) ;
89+ let user = JSON . parse ( sessionStorage . getItem ( 'user' ) ) ;
90+ if ( ! user && to . path != '/login' ) {
91+ next ( { path : '/login' } )
92+ }
8993 next ( )
9094} )
9195
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ const LoginUsers = [
44 id : 1 ,
55 username : 'admin' ,
66 password : '123456' ,
7- avatar : '' ,
7+ avatar : 'https://raw.githubusercontent.com/taylorchen709/markdown-images/master/vueadmin/user.png ' ,
88 name : '张某某'
99 }
1010] ;
Original file line number Diff line number Diff line change 77</el-col >
88<el-col :span =" 4" class =" rightbar" >
99 <el-dropdown trigger =" click" >
10- <span class =" el-dropdown-link" style =" color :#c0ccda ;cursor : pointer ;" ><img src =" ../assets/user.png " class =" head" > 张某某
10+ <span class =" el-dropdown-link" style =" color :#c0ccda ;cursor : pointer ;" ><img : src =" this.sysUserAvatar " class =" head" > {{sysUserName}}
1111</span >
1212<el-dropdown-menu slot =" dropdown" >
1313 <el-dropdown-item >我的消息</el-dropdown-item >
8282 currentPath: ' /table' ,
8383 currentPathName: ' Table' ,
8484 currentPathNameParent: ' 导航一' ,
85+ sysUserName: ' ' ,
86+ sysUserAvatar: ' ' ,
8587 form: {
8688 name: ' ' ,
8789 region: ' ' ,
119121 this .$confirm (' 确认退出吗?' , ' 提示' , {
120122 // type: 'warning'
121123 }).then (() => {
124+ sessionStorage .removeItem (' user' );
122125 _this .$router .replace (' /login' );
123126 }).catch (() => {
124127
125128 });
126129
127130
131+ }
132+ },
133+ mounted () {
134+ this .currentPath = this .$route .path ;
135+ this .currentPathName = this .$route .name ;
136+ this .currentPathNameParent = this .$route .matched [0 ].name ;
137+
138+ var user = sessionStorage .getItem (' user' );
139+ if (user) {
140+ user = JSON .parse (user);
141+ this .sysUserName = user .name || ' ' ;
142+ this .sysUserAvatar = user .avatar || ' ' ;
128143 }
129144 }
130145 }
Original file line number Diff line number Diff line change 6262 type: ' error'
6363 });
6464 } else {
65- localStorage .setItem (' user' , JSON .stringify (user));
66- if (this .$route .query .redirect ) {
67- this .$router .push ({ path: this .$route .query .redirect });
68- } else {
69- this .$router .push ({ path: ' /table' });
70- }
65+ sessionStorage .setItem (' user' , JSON .stringify (user));
66+ this .$router .push ({ path: ' /table' });
7167 }
7268 });
7369 } else {
You can’t perform that action at this time.
0 commit comments