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

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

頁面重新加載時Vue getter返回未定義

頁面重新加載時Vue getter返回未定義

catspeake 2022-06-16 15:31:52
我有一個博客,里面有一些帖子。當您單擊預覽時,您將重定向到頁面帖子。在帖子的頁面上,我使用 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>
查看完整描述

3 回答

?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

您正在嘗試validate未定義的屬性。所以你需要先檢查ricetta。

試試這樣:

<div v-if="ricetta && ricetta.validate === true" id="sezione-ricetta">


查看完整回答
反對 回復 2022-06-16
?
森欄

TA貢獻1810條經驗 獲得超5個贊

數據庫同步是異步的,ricettario最初是一個空數組。ricettario一旦同步完成并填充數組,將重新計算計算值,更新組件。

即使ricettario不是空的,如果它什么也沒找到,find可能會返回undefined。這需要在使用的地方處理ricetta

<div v-if="ricetta && ricetta.validate" id="sezione-ricetta">


查看完整回答
反對 回復 2022-06-16
?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

錯誤日志非常明確,xxx.validate您的組件模板中有一個地方Ricetta,但xxx未定義。

因此,您的應用程序崩潰并停止工作。我懷疑它與 Vuex 有什么關系


查看完整回答
反對 回復 2022-06-16
  • 3 回答
  • 0 關注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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