我有一個博客,里面有一些帖子。當您單擊預覽時,您將重定向到頁面帖子。在帖子的頁面上,我使用 getter 加載正確的帖子(我使用find函數返回object.name對應于對象數組中的正確對象)。const state = { ricettario: [], // data that contains all recipes (array of objects)}const actions = { // Bind State and Firestore collection init: firestoreAction(({ bindFirestoreRef }) => { bindFirestoreRef('ricettario', db.collection('____').orderBy('data')) })const getters = { caricaRicetta(state) { console.log('Vuex Getter FIRED => ', state.ricettario) return nameParamByComponent => state.ricettario.find(ricetta => { return ricetta.name === nameParamByComponent }) }}在組件中,我在computed propertycomputed: { ...mapGetters('ricettaStore', ['caricaRicetta']), ricetta() { return this.caricaRicetta(this.slug) // this.slug is the prop of the URL (by Router) } }一切都以正確的方式進行,但是當我在 POST PAGE 中重新加載頁面時,getter 將觸發 2 次:1. 因為狀態為空而返回錯誤2. 返回正確的對象// 下面的屏幕因此,從正面看一切正常,但在控制臺和應用程序中卻完全不行。我認為正確的方法是在created鉤子中調用 getter。我要改變什么?計算的道具、吸氣劑或狀態有問題嗎?發布頁面:<template> <div v-if="ricetta.validate === true" id="sezione-ricetta"> <div class="container"> <div class="row"> <div class="col s12 m10 offset-m1 l8 offset-l2"> <img class="img-fluid" :src="ricetta.img" :alt="'Ricetta ' + ricetta.titolo" :title="ricetta.titolo" /> </div> </div> </div> </div> <div v-else> ... </div></template>
頁面重新加載時Vue getter返回未定義
catspeake
2022-06-16 15:31:52