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

获取到数据(对象、数组),截取一部分显示到页面中,用computed计算属性来实现截取数据然后直接输出到页面。

<div class="detailBox">
  <h1 class="detailHead">{{ActiveData.title}}</h1>
  <div class="detailCon">
    <p><b>活动时间:</b>{{ActStart}} 至 {{ActEnd}}</p>
    <p><b>报名时间:</b>{{SigStart}} 至 {{SigEnd}}</p>
  </div>
</div>
data() {
   return {
     ActiveData:"",//活动详情所有数据
  }
},
methods:{
//获取对应的数据
    this.ActiveData = response.data.data;
}
computed:{
    ActStart(){
      console.log(this.ActiveData.activity_starttime);
      return this.ActiveData.activity_starttime.substring(5,11);
    },
    ActEnd(){
      return this.ActiveData.activity_endtime.substring(5,11);
    },
    SigStart(){
     return this.ActiveData.signup_starttime.substring(5,11);
    },
    SigEnd(){
      return this.ActiveData.signup_endtime.substring(5,11);
    },
  }

页面如愿显示出想要的效果了,但是也报错了!那是因为data里的数据是在mouted中执行函数才获取到数据,是在computed之后,所以在第一次computed计算时,data中的ActiveData数据还是空的,所以computed找不到ActiveData里的数据。就会报undefinded接着是Error in render: "TypeError:……"获取到值后重新计算又出现了获取到的值。

解决方法一:

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

data() {
    return {
      ActiveData:"",//活动详情所有数据
      ActStarts:"",//活动开始时间
      ActEnds:"",//活动结束时间
      SigStarts:"",//报名开始时间
      SigEnds:"",//报名结束时间
    }
  },


methods:{
//获取对应的数据
    this.ActiveData = response.data.data;
    this.ActStarts = response.data.data.activity_starttime;
    this.ActEnds = response.data.data.activity_endtime;
    this.SigStarts = response.data.data.signup_starttime
    this.SigEnds = response.data.data.signup_endtime
}
computed:{
    ActStart(){
      console.log(this.ActStarts);
      return this.ActStarts.substring(5,11);
    },
    ActEnd(){
      return this.ActEnds.substring(5,11);     
    },
    SigStart(){
      return this.SigStarts.substring(5,11);
    },
    SigEnd(){
      return this.SigEnds.substring(5,11);
    },
  }

解决方法二:

在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);
      }
   },
}

 

拓展阅读vue的computed计算属性

 

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值