Vue中computed計算屬性和data數(shù)據(jù)獲取方式
computed計算屬性和data數(shù)據(jù)獲取
獲取到數(shù)據(jù)(對象、數(shù)組),截取一部分顯示到頁面中,用computed計算屬性來實現(xiàn)截取數(shù)據(jù)然后直接輸出到頁面。
<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:"",//活動詳情所有數(shù)據(jù)
}
},
methods:{
//獲取對應的數(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);
},
}頁面如愿顯示出想要的效果了,但是也報錯了!那是因為data里的數(shù)據(jù)是在mouted中執(zhí)行函數(shù)才獲取到數(shù)據(jù),是在computed之后,所以在第一次computed計算時,data中的ActiveData數(shù)據(jù)還是空的,所以computed找不到ActiveData里的數(shù)據(jù)。
就會報undefinded接著是Error in render: "TypeError:……"獲取到值后重新計算又出現(xiàn)了獲取到的值。

解決方法一
給要截取的數(shù)據(jù)賦一個默認值,computed計算屬性會先去計算默認值,在獲取到新值后重新計算,就不會報undefinded的錯誤了。
data() {
return {
ActiveData:"",//活動詳情所有數(shù)據(jù)
ActStarts:"",//活動開始時間
ActEnds:"",//活動結(jié)束時間
SigStarts:"",//報名開始時間
SigEnds:"",//報名結(jié)束時間
}
},
methods:{
//獲取對應的數(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ù)時才在computed中返回對應的數(shù)據(jù)
data() {
return {
ActiveData:"",//活動詳情所有數(shù)據(jù)
}
},
methods:{
//獲取對應的數(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計算屬性取對象的值,第一次報錯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
// }
}
},圖片:

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

解決方案
那我們現(xiàn)在可以知道,報錯的原因是第一次計算的時候,addressList還沒有賦值,所以,我們可以進行一個判斷,當addressList有值了我們在進行計算

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
BeanUtils.copyProperties復制屬性失敗的原因及解決方案
這篇文章主要介紹了BeanUtils.copyProperties復制屬性失敗的原因及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
Spring Data Envers支持有條件變動紀錄的保存和查詢的方法
通過spring-data-envers可以很容易的實現(xiàn)數(shù)據(jù)變動紀錄的保存和查詢,本文介紹支持有條件變動紀錄的保存和查詢的方法,通過spring-data-envers很容易的實現(xiàn)變動紀錄的保存和查詢,只需要增加幾個注解就可以,感興趣的朋友跟隨小編一起看看吧2023-10-10
Java concurrency線程池之Callable和Future_動力節(jié)點Java學院整理
這篇文章主要為大家詳細介紹了Java concurrency線程池之Callable和Future,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
解決Eclipse Tomcat OutOfMemoryError:PermGen space的問題
今天小編就為大家分享一篇關于解決Eclipse Tomcat OutOfMemoryError:PermGen space的問題,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12

