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:{ //獲取對應(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); }, }
頁面如愿顯示出想要的效果了,但是也報錯了!那是因為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ù)賦一個默認(rèn)值,computed計算屬性會先去計算默認(rèn)值,在獲取到新值后重新計算,就不會報undefinded的錯誤了。
data() { return { ActiveData:"",//活動詳情所有數(shù)據(jù) ActStarts:"",//活動開始時間 ActEnds:"",//活動結(jié)束時間 SigStarts:"",//報名開始時間 SigEnds:"",//報名結(jié)束時間 } }, methods:{ //獲取對應(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ù)時才在computed中返回對應(yīng)的數(shù)據(jù)
data() { return { ActiveData:"",//活動詳情所有數(shù)據(jù) } }, methods:{ //獲取對應(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計算屬性取對象的值,第一次報錯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還沒有賦值,所以,我們可以進(jìn)行一個判斷,當(dāng)addressList有值了我們在進(jìn)行計算
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java配置變量的解釋,搬運(yùn)他人優(yōu)質(zhì)評論(推薦)
這篇文章主要介紹了java配置變量,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04使用?mybatis?自定義日期類型轉(zhuǎn)換器的示例代碼
這篇文章主要介紹了使用?mybatis?自定義日期類型轉(zhuǎn)換器的示例代碼,這里使用mybatis中的typeHandlers?實現(xiàn)的,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03