datalist是原始數(shù)據(jù),datafilter是過濾之后的數(shù)據(jù),用datafilter去做循環(huán)
建立兩個空數(shù)組,第一次請求,兩個數(shù)組都有當(dāng)月數(shù)據(jù),然后點(diǎn)擊日期的那個方法執(zhí)行之后,采用原始數(shù)據(jù)過濾之后給datafilter,
我想知道大家還有更好的方式實現(xiàn)嘛?
<div class="d-list-cont" v-if="datafilter.length>0">
<div class="d-list" v-for="(item,index) in datafilter" :key="index">
........................
</div>
</div>
<div class="no-data" v-else>
<div class="img"></div>
</div>
data() {
return {
datalist:[],
datafilter:[],
}
},
methods:{
getData(){
this.$get('接口地址',請求參數(shù))
.then(response=> {
this.datalist=response
this.datafilter=response
})
.catch(error=> {
//alert('錯誤')
});
},
//點(diǎn)擊日期的方法
clickDay(data) {
//把2018/07/26 格式化符合后端需求格式一樣的
var a = data.split('/');
var b = a[0] + '-' + (a[1] < 10 ? '0':'') + a[1]+'-'+ (a[2] < 10 ? '0':'')+a[2];
if(this.datalist){
this.datafilter=this.datalist.filter(o=>o.recoverTime===b)
}
},
}
我解釋一下上面關(guān)于computed
的評論吧
Vue是響應(yīng)式的,這就意味著一個好的設(shè)計模式是只保存源數(shù)據(jù),進(jìn)一步的變換交給框架。換言之,就是不增加沒有必要的狀態(tài)。所以我的建議是把datafilter
放到computed
里面。
{
data() {
return {
datalist: [],
// filter就應(yīng)該存filter,如果按原來的邏輯,應(yīng)該叫filteredDataList
// 其實我更喜歡selectedDate,更加明確
// datafilter: null,
selectedDate: null,
}
},
computed: {
filteredDatalist() {
if (this.selectedDate) {
return this.datalist.filter(({ recoverTime }) => recoverTime === this.selectedDate);
}
else {
return this.datalist;
}
}
},
methods: {
getData() {
this.$get('接口地址', 請求參數(shù))
.then(response => {
this.datalist = response
})
.catch(error => {
});
},
clickDay(date) {
var a = date.split('/');
// 用樓上的方式更優(yōu)雅一些
this.selectedDate = a[0] + '-' + (a[1] < 10 ? '0' : '') + a[1] + '-' + (a[2] < 10 ? '0' : '') + a[2];
},
},
}
好處都有啥?誰說對了就給他
datalist
生命周期的限制。假如以后想要加上datalist
實時更新的功能,只需要定時getData
就行了(或者用websocket,不論是啥),不需要用戶觸發(fā)clickDay
就能自動刷新;computed
屬性的緩存,如果用戶clickDay
兩次都相同,只會計算一次。其實這種思想已經(jīng)很函數(shù)式了,相當(dāng)像Rx。但是Vue的學(xué)習(xí)成本比Rx低得多,這么簡單就能用上函數(shù)式,何樂不為?
我就覺得這兩句可以換一個優(yōu)雅點(diǎn)的方式
var a = data.split('/');
var b = a[0] + '-' + (a[1] < 10 ? '0':'') + a[1]+'-'+ (a[2] < 10 ? '0':'')+a[2];
改為
// 如果你的數(shù)據(jù)結(jié)構(gòu)是2018/07/26,為什么還要去判斷加0,直接用replace替換不好嗎
// data.replace(/\//g, '-')
// 加0寫法
const b = `${a[0]}-${a[1].padStart(2, '0')}-${a[2].padStart(2, '0')}`;
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。