如何处理Vue中存储的空数据

时间:2018-09-14 03:01:01

标签: vue.js nuxt.js

如果数据之一为空,当前我收到错误消息。数据从vuex存储获取。如何清除错误?

.AgeRange {
  width: 30%;
  margin-bottom: 20px;
}
.pl2 {
padding-left: 10px;
}
.AgeNum {
  position: relative;
  top: 20px;
  right: 23%;
}
.AgeRangeLabel {
  margin: 10px 0;
  color:#0b867a;
}
.AgeRangeDiv {
  border: 1px solid $ee;
  background: $ff;
  padding: 3px 5px 5px 12px;
  border-radius: 4px;
}

2 个答案:

答案 0 :(得分:0)

created() 钩子之前使用 this 实例不是一个好习惯, em> data() 块,因此在after或如下所示的已创建钩子中设置值

data (){
    return{
      projects: null
    }
  },
created(){
this. projects =this.$store.state.authUser.roles
}

N.B:最好使用getter获取存储数据

this.$store.getters.AUTH_USER_ROLES


const store = new Vuex.Store({
  state: {
    authUser: {
roles:[]
}
  },
  getters: {
    AUTH_USER_ROLES: state => state.authUser.roles,
  }
})

答案 1 :(得分:-2)

像这样吗?

data (){
  return{
    projects: this.$store.state.authUser && this.$store.state.authUser.roles
  }
},