【问题标题】:Which lifecycle hook to use on child component to run after parent in Vuejs?在子组件上使用哪个生命周期钩子在 Vuejs 中的父组件之后运行?
【发布时间】:2020-10-09 11:13:01
【问题描述】:

在我正在运行的父级上:

  mounted(){
    this.$store.dispatch('fetchNotesAction')
  },
components: { ChildComponent }

这样,Vuex 存储就会填充数据。

因此,我假设在子组件运行时,商店已经满了。

ChildComponent我正在尝试这个

mounted(){
    console.log(this.$store.getters.getNotes) 
  },

如果我在父项中记录完全相同的代码,则在 dispatch 之后它会显示数据。所以这些行后面的代码(连接)工作正常,只是我假设我不需要再次重新运行提取(来自fetchNotesAction)是正确的吗?

我试过 created 而不是 mounted 和同样的事情

【问题讨论】:

    标签: vue.js vue-component vuex


    【解决方案1】:

    最佳做法是使用计算属性返回您的状态 getter,然后使用 watch 属性观察其在子组件中的变化:

    computed:{
      notes(){
       return  this.$store.getters.getNotes;
       }
    },
    watch:{
      notes(newval,oldVal)'
        console.log(newval) 
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-18
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      • 2016-01-09
      相关资源
      最近更新 更多