使用vue-cli創(chuàng)建的項(xiàng)目,父組件App.vue里加載了子組件index.vue,父組件與子組件的methods里各自有個(gè)方法。預(yù)設(shè)的想法是想在父組件App.vue的方法里把a(bǔ)jax請(qǐng)求到的數(shù)據(jù)設(shè)置成緩存,然后子組件index.vue的方法去使用緩存的數(shù)據(jù)。但調(diào)試結(jié)果發(fā)現(xiàn),無(wú)論父組件App.vue里的方法放在函數(shù)生命周期里的哪個(gè)鉤子里,執(zhí)行的順序總是慢于子組件index.vue的方法,導(dǎo)致子組件里調(diào)用的緩存數(shù)據(jù)是undefined,結(jié)果報(bào)錯(cuò)。詳情如下圖
父組件代碼如下:
<template>
<div id="app">
<router-view/>
</div>
</template>
<script>
import bus from '../static/js/bus.js'
export default {
name: 'app',
data () {
return {
}
},
methods: {
checkLoingStatus () {
//首次進(jìn)入頁(yè)面初始化默認(rèn)未登錄并跳轉(zhuǎn)到登錄頁(yè)面
if (!this.currentLoginStatus && !this.loginStatus) {
App.setStorage('loginStatus', {'loginStatus': false}, 'localStorage')
window.location.href = "#/signIn"
}
},
getData () {
$.ajax({
url: '../../static/data/data.json',
type: 'get',
success: (response) => {
console.log('=== ' + response.message + ' ===')
console.log('=== 緩存基礎(chǔ)配置數(shù)據(jù)成功 ===');
//此處設(shè)置基礎(chǔ)數(shù)據(jù)緩存response.data
App.setStorage('response', {'data': response.data}, 'localStorage')
},
error: (response) => {
//...
}
})
}
},
beforeCreate:function(){//組件實(shí)例化之前
console.log('1===beforeCreate===')
},
created:function(){//組件實(shí)例化了
console.log('2===created===')
},
beforeMount:function(){//組件寫(xiě)入dom結(jié)構(gòu)之前
console.log('3===beforeMount===')
},
mounted:function(){//組件寫(xiě)入dom結(jié)構(gòu)了
console.log('4===mounted===')
//基礎(chǔ)配置數(shù)據(jù)
this.getData()
},
beforeUpdate:function(){//組件更新前
console.log('5===beforeUpdate===')
},
updated:function(){//組件更新比如修改了文案
console.log('6===updated===')
},
beforeDestroy:function(){//組件銷(xiāo)毀之前
console.log('7===beforeDestroy===')
},
destroyed:function(){//組件已經(jīng)銷(xiāo)毀
console.log('8===destroyed===')
}
}
</script>
子組件代碼如下:
<template>
<div class="page">
<!-- 省略 -->
</div>
</template>
<script>
export default {
name: 'index',
data() {
return {
}
},
components: {
},
methods: {
getStorageResponseData () {
//獲取緩存的基礎(chǔ)配置數(shù)據(jù)
console.log('=== 開(kāi)始使用緩存里的基礎(chǔ)配置數(shù)據(jù) ===')
this.responseData = App.getStorage('response').data
}
},
mounted () {
this.$nextTick(() => {
//由于getStorageResponseData比父組件的設(shè)置緩存方法先執(zhí)行
//結(jié)果獲取不到緩存數(shù)據(jù),報(bào)undefined
this.getStorageResponseData()
})
}
}
</script>
思路好像有點(diǎn)問(wèn)題,因?yàn)閍jax異步請(qǐng)求,導(dǎo)致你這個(gè)緩存其實(shí)不是最優(yōu)的傳遞方式,子父類(lèi)組件推薦用
1、prop傳遞prop文檔
2、傳遞進(jìn)去后,可以在子組件內(nèi)調(diào)用updated 鉤子函數(shù)進(jìn)行渲染,可以解決異步的問(wèn)題
我今天也遇到了類(lèi)似的問(wèn)題,在父組件created時(shí)異步請(qǐng)求數(shù)據(jù)并存入vuex,子組件created時(shí)使用vuex里的數(shù)據(jù)初始化,結(jié)果子組件初始化時(shí)vuex里的數(shù)據(jù)是空的。后來(lái)發(fā)現(xiàn)可以在子組件里通過(guò)watch監(jiān)聽(tīng)vuex里的屬性試試,結(jié)果成功啦~
不過(guò)話說(shuō)回來(lái)如果像樓主一樣在不用vuex的情況下,直接在父組件鉤子函數(shù)this.getData()之后再通過(guò)this.$refs.child.getStorageResponseData()調(diào)用子組件的方法應(yīng)該可以解決問(wèn)題。
本題的初衷是把父組件App.vue與子組件index.vue的配置數(shù)據(jù)全部都寫(xiě)在一個(gè)data.json文件,然后在父組件App.vue通過(guò)一次ajax請(qǐng)求到數(shù)據(jù),在App.vue使用數(shù)據(jù)的同時(shí),把數(shù)據(jù)緩存起來(lái),給子組件index.vue去使用,以到達(dá)減少ajax請(qǐng)求次數(shù),但發(fā)現(xiàn)這樣無(wú)法實(shí)現(xiàn)。所以,還是回歸原始解決方法,把data的配置數(shù)據(jù)分開(kāi)成兩個(gè)json文件,分別在父組件與子組件去調(diào)用ajax獲取并分別使用。
北大青鳥(niǎo)APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專(zhuān)業(yè)的國(guó)家
北大青鳥(niǎo)中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(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),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。