亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

萌新!vue 通過請求獲取數據return回來,不好使各位有什么建議?

萌新!vue 通過請求獲取數據return回來,不好使各位有什么建議?

料青山看我應如是 2019-09-08 21:37:14
vue里面在data里面定義了變量,想要通過http請求獲取,使用return不好使,刷新的時候取緩存好使直接this.獲取好使,但是this.代碼量比較大,還是希望return可以解決相關代碼created(){this.sydLb=this.getEnumTypeDict({parentcode:'01'})this.sydJb=this.getEnumTypeDict({parentcode:'04'})},methods:{getEnumTypeDict(params){if(JSON.parse(window.localStorage.getItem(`sydJb${params.parentcode}`))){returnJSON.parse(window.localStorage.getItem(`sydJb${params.parentcode}`))}api.getEnumTypeDict(params).then(res=>{if(res.status===200&&res.data.code){setLocalStorage(`sydJb${params.parentcode}`,res.data.data.data)returnres.data.data.data}}).catch(error=>{console.log(error)})}}希望可以解決這個問題,使用this寫兩個方法實在有點麻煩
查看完整描述

2 回答

?
侃侃無極

TA貢獻2051條經驗 獲得超10個贊

asynccreated(){
this.sydLb=awaitthis.getEnumTypeDict({parentcode:'01'})
this.sydJb=awaitthis.getEnumTypeDict({parentcode:'04'})
},
methods:{
asyncgetEnumTypeDict(params){
try{
if(JSON.parse(window.localStorage.getItem(`sydJb${params.parentcode}`))){
returnJSON.parse(window.localStorage.getItem(`sydJb${params.parentcode}`))
}
constres=api.getEnumTypeDict(params)
if(res.status===200&&res.data.code){
setLocalStorage(`sydJb${params.parentcode}`,res.data.data.data)
returnres.data.data.data
}
}
catch(e){
//錯誤處理
}
}
}
                            
查看完整回答
反對 回復 2019-09-08
  • 2 回答
  • 0 關注
  • 431 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號