问题:
打印this.$store.getters可以获取的到,但是打印this.$store.getters.authInfo就是undefined
console.log(this.$store.getters,'是否登录')
console.log(this.$store.getters.authInfo,'是否登录')
原因:
因为actions是异步操作,你在commit方法里再去打印肯定就有值了
为什么对象能打印到?因为对象那个变量是地址,你展开的时候对象已经扩充了,所以能看到。
对象变量都是对象在内存中的地址值,普通数据变量是实际的值。所以你打印的那个对象其实在打印的那一刻是空对象,但是你展开的时候,commit操作已经完成,所以对象被扩展为你期望看到的值。但是普通类型打印的那一刻是什么就是什么。
解决方法:
放在异步setTimeout函数中
setTimeout(()=>{
console.log(this.$store.getters,'是否登录')
console.log(this.$store.getters.authInfo,'是否登录')
console.log(this.$store.getters.authInfo.alreadyBinded,'是否登录')
},500); // 时间可以写长一点,一开始写200也是不行的