写vue项目时从store拿值说state.user is undefined定义的state.user不存在如何解决,store代码如下import { createStore } from 'vuex' export default createStore({ state: { name: "store1111", user: {} }, getters: { getName(state) { console.log(state) return '1111' }, getUname(state) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore return state.user.username }, getSex(state){ // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore return state.user.sex }, getRid(state) { // @ts-ignore return state.user.rid } }, mutations: { setName(state, data) { state.name = data }, login(state, data) { sessionStorage.setItem('user', JSON.stringify(data)) state.user = data } }, actions: { }, modules: { } })
时间: 2024-02-14 14:15:15 浏览: 33
这种情况可能是因为你在调用 `state.user` 时,`state.user` 还没有被赋值,所以它是 `undefined`。要解决这个问题,你可以在调用 `state.user` 之前先进行一次判断,检查 `state.user` 是否存在。你可以使用 `if` 语句或者三目运算符来实现。例如:
```
getUname(state) {
if (state.user && state.user.username) {
return state.user.username
} else {
return ''
}
},
```
或者
```
getUname: state => state.user && state.user.username ? state.user.username : ''
```
这样就可以避免出现 `state.user` 不存在的问题了。
相关问题
vue-admin-template中,怎么设置store.state.user.role的值?
在 Vuex 中,通常使用 mutation 来改变 store 中的状态。在 vue-admin-template 中,您可以定义一个 mutation 来改变 store.state.user.role 的值,例如:
```
// store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
user: {
role: ''
}
},
mutations: {
SET_ROLE (state, role) {
state.user.role = role
}
}
})
```
然后,在组件中,您可以使用 `$store.commit` 方法来触发该 mutation,并传入新的 role 值:
```
// someComponent.vue
import { mapMutations } from 'vuex'
export default {
methods: {
...mapMutations(['SET_ROLE']),
updateRole () {
this.SET_ROLE('admin')
}
}
}
```
您还可以使用 `mapMutations` 辅助函数将组件中的 methods 与 store 中的 mutations 建立映射,这样就可以在组件中直接使用 `this.SET_ROLE` 来触发 mutation。
注意:mutation 中的方法应该是同步的,因为它们是用来改变 store 中的状态的。如果您需要执行异步操作,可以使用 action。
this.$store.state.user.name
这段代码可能是在Vue.js中使用Vuex状态管理库时使用的,其中`this.$store`是Vuex实例,`state`是Vuex中的数据仓库,`user`是存储用户信息的一个对象,`name`则是该用户对象中的一个属性,可以通过`this.$store.state.user.name`来获取该属性的值。具体来说,`this.$store.state`是Vuex数据仓库中的状态对象,`user`是其中的一个属性,而`name`则是`user`对象的一个属性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)