VUE中computed计算属性和data数据获取的问题

问题描述:

数据是从后台获取的,想在计算属性中,对data数据截取,但是报错未定义在这里插入图片描述
原因:那是因为data里的数据是在mouted中执行函数才获取到数据,是在computed之后,所以在第一次computed计算时,data中数据还是空的,所以computed找不到data里的数据。就会报undefinded接着是Error in render: "TypeError:……"获取到值后重新计算又出现了获取到的值。

解决方法一:

给要截取的数据赋一个默认值,computed计算属性会先去计算默认值,在获取到新值后重新计算,就不会报undefinded的错误了。

解决方法二:

在computed中增加if判断,在data中的ActiveData里有数据时才在computed中返回对应的数据

data() {
   return {
     ActiveData:"",//活动详情所有数据
  }
},
methods:{
//获取对应的数据
    this.ActiveData = response.data.data;
}
computed:{
    ActStart(){
      console.log(this.ActiveData.activity_starttime);
      if(this.ActiveData.activity_starttime !== undefined){
        return this.ActiveData.activity_starttime.substring(5,11);
      }
   },
   ActEnd(){
      if(this.ActiveData.activity_endtime !== undefined){
        return this.ActiveData.activity_endtime.substring(5,11);
      }
   },
   SigStart(){
      if(this.ActiveData.signup_starttime !== undefined){
        return this.ActiveData.signup_starttime.substring(5,11);
      }
   },
   SigEnd(){
       if(this.ActiveData.signup_endtime !== undefined){
        return this.ActiveData.signup_endtime.substring(5,11);
      }
   },
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值