Vue中computed計(jì)算屬性和data數(shù)據(jù)獲取方式
computed計(jì)算屬性和data數(shù)據(jù)獲取
獲取到數(shù)據(jù)(對(duì)象、數(shù)組),截取一部分顯示到頁面中,用computed計(jì)算屬性來實(shí)現(xiàn)截取數(shù)據(jù)然后直接輸出到頁面。
<div class="detailBox">
<h1 class="detailHead">{{ActiveData.title}}</h1>
<div class="detailCon">
<p><b>活動(dòng)時(shí)間:</b>{{ActStart}} 至 {{ActEnd}}</p>
<p><b>報(bào)名時(shí)間:</b>{{SigStart}} 至 {{SigEnd}}</p>
</div>
</div>data() {
return {
ActiveData:"",//活動(dòng)詳情所有數(shù)據(jù)
}
},
methods:{
//獲取對(duì)應(yīng)的數(shù)據(jù)
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);
},
}頁面如愿顯示出想要的效果了,但是也報(bào)錯(cuò)了!那是因?yàn)閐ata里的數(shù)據(jù)是在mouted中執(zhí)行函數(shù)才獲取到數(shù)據(jù),是在computed之后,所以在第一次computed計(jì)算時(shí),data中的ActiveData數(shù)據(jù)還是空的,所以computed找不到ActiveData里的數(shù)據(jù)。
就會(huì)報(bào)undefinded接著是Error in render: "TypeError:……"獲取到值后重新計(jì)算又出現(xiàn)了獲取到的值。

解決方法一
給要截取的數(shù)據(jù)賦一個(gè)默認(rèn)值,computed計(jì)算屬性會(huì)先去計(jì)算默認(rèn)值,在獲取到新值后重新計(jì)算,就不會(huì)報(bào)undefinded的錯(cuò)誤了。
data() {
return {
ActiveData:"",//活動(dòng)詳情所有數(shù)據(jù)
ActStarts:"",//活動(dòng)開始時(shí)間
ActEnds:"",//活動(dòng)結(jié)束時(shí)間
SigStarts:"",//報(bào)名開始時(shí)間
SigEnds:"",//報(bào)名結(jié)束時(shí)間
}
},
methods:{
//獲取對(duì)應(yīng)的數(shù)據(jù)
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里有數(shù)據(jù)時(shí)才在computed中返回對(duì)應(yīng)的數(shù)據(jù)
data() {
return {
ActiveData:"",//活動(dòng)詳情所有數(shù)據(jù)
}
},
methods:{
//獲取對(duì)應(yīng)的數(shù)據(jù)
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);
}
},
}computed計(jì)算屬性取對(duì)象的值,第一次報(bào)錯(cuò)undefined
代碼如下:
data() {
return {
limit:3,
checkedIndex:0,
addressList:[],
isMdShow:false,
addressId:""
};
},
components: {
NavHeader,
NavFooter,
NavBread,
Modal
},
mounted(){
this.init()
},
computed:{
addressListFilter(){
return this.addressList.slice(0,this.limit)
},
selectedAddressId(){
// if(this.addressList.length>0){
let data = this.addressList[this.checkedIndex].addressId
console.log(this.addressList,"====")
return data
// }
}
},圖片:

注意,初始化的時(shí)候,addressList還是一個(gè)空數(shù)組,那addressList[index]對(duì)象就不存在,肯定就沒有addressId這個(gè)屬性,所以會(huì)報(bào)undefined
報(bào)錯(cuò)和打印值

解決方案
那我們現(xiàn)在可以知道,報(bào)錯(cuò)的原因是第一次計(jì)算的時(shí)候,addressList還沒有賦值,所以,我們可以進(jìn)行一個(gè)判斷,當(dāng)addressList有值了我們?cè)谶M(jìn)行計(jì)算

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
BeanUtils.copyProperties復(fù)制屬性失敗的原因及解決方案
這篇文章主要介紹了BeanUtils.copyProperties復(fù)制屬性失敗的原因及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Spring Data Envers支持有條件變動(dòng)紀(jì)錄的保存和查詢的方法
通過spring-data-envers可以很容易的實(shí)現(xiàn)數(shù)據(jù)變動(dòng)紀(jì)錄的保存和查詢,本文介紹支持有條件變動(dòng)紀(jì)錄的保存和查詢的方法,通過spring-data-envers很容易的實(shí)現(xiàn)變動(dòng)紀(jì)錄的保存和查詢,只需要增加幾個(gè)注解就可以,感興趣的朋友跟隨小編一起看看吧2023-10-10
Java concurrency線程池之Callable和Future_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java concurrency線程池之Callable和Future,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Effective Java 在工作中的應(yīng)用總結(jié)
《Effective Java》是一本經(jīng)典的 Java 學(xué)習(xí)寶典,值得每位 Java 開發(fā)者閱讀。下面文章即是將書中和平日工作較密切的知識(shí)點(diǎn)做了部分總結(jié),需要的朋友可以參考下2021-09-09
解決Eclipse Tomcat OutOfMemoryError:PermGen space的問題
今天小編就為大家分享一篇關(guān)于解決Eclipse Tomcat OutOfMemoryError:PermGen space的問題,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
springboot Mongodb的集成與使用實(shí)例詳解
這篇文章主要介紹了springboot Mongodb的集成與使用實(shí)例詳解,需要的朋友可以參考下2018-04-04

