第一次摸索着完成vue的权限管理,在此记录下自己搬砖的点点滴滴,希望以后回望的时候,有迹可循,也分享给需要的小伙伴们,参考哦~
思路如下:
1,登录成功后,把后台返回的用户信息,存vueX的state;
2,vueX的actions调接口,返回数据后,赋值给state,然后调用递归,来把返回的数据处理成我们要的格式
3,permission.js中router.beforeEach(),调actions的方法,这里用到了异步执行,所以,需要调用actions的方法后,才能进行下一步,否则,会导致跳转失效
<!-- html的部分-->
<el-button type="primary" @click="l`在这里插入代码片`ogin()">登陆</el-button>
// js的部分
login() {
```````````````````````````````````````````
// 请求接口
api.login(params).then(res => {
if (res.code == 200) {
// 请求到数据后,存入token和id
setToken(res.object.token, res.object.id);
// 存用户名和姓名
setUserName(res.object.name, res.object.username);
`````````````````````````````
// 1,登录成功后,把后台返回的用户信息,存vueX的state;
if (res.object.sysNewMenuVoList.length > 0) {
this.$store.commit("GetUserInfo", res.object.sysNewMenuVoList);
} else {
// 否则不让登陆
this.$router.push("/login");
this.$message.error(<