大佬们,localStorage获取值后有点疑惑?
loginName: JSON.parse(localStorage.getItem("loginName") ? localStorage.getItem("loginName") : {})
created() { console.log(this.loginName) //1
const loginName = this.loginName
if (!loginName) {
this.$message.error("当前无法获取用户信息")
return
}
this.axios.get('/db1/Student/informationManage/' + loginName).then((resp) => {
console.log(resp.data.content);
})
},
后端:
@Override public SysUserPojo getinformationManage(String loginName) {
QueryWrapper<SysUserPojo> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("login_name",loginName);
SysUserPojo list = sysUserMapper.selectOne(queryWrapper);
return list;
}
@GetMapping("/Student/informationManage/{loginName}") public CommonResp informationManage(@PathVariable String loginName){
CommonResp<SysUserPojo> resp = new CommonResp<>();
SysUserPojo list = sysStudentService.getinformationManage(loginName);
//返回泛型数据 自定义类型
//private T content;
resp.setContent(list);
return resp;
}
resp.data.content:{loginName: '1', name: '1', permission: 1, clas: null}
resp.data.content[0]:undefined
this.tableData必须得等于resp.data.content[0],这该怎么获取值
回答:
resp.data.content = { 0 : undefined , ... }
属性名为0并没有赋值;应先赋值。
还是说是想获取 {loginName: '1', name: '1', permission: 1, clas: null} 这个对象?
那这样就行 this.tableData = resp.data.content;
回答:
JS里没有办法直接通过索引的下标拿到对象属性的值,也就是说你这样写
resp.data.content[0]相当于要拿对象里面key值为"0"的值
你可以这样使用:
resp.data.content.loginName 或 resp.data.content['loginName']
以上是 大佬们,localStorage获取值后有点疑惑? 的全部内容, 来源链接: utcz.com/p/934004.html