简体   繁体   English

异步状态更改后Vuex执行功能

[英]Vuex Executing Function After Async State Change

I've become frustrated with finding an example for what I thought would be a really common use case for something like Vuex. 我为找到一个我认为像Vuex之类的常见用例的例子而感到沮丧。

I dispatch an asynchronous action on the store to populate it via api. 我在商店上调度异步操作,通过api填充它。 Once this store is populated I need to then execute certain operations. 填充此商店后,我需要执行某些操作。

Every Vuex example I come across seems to just deal with a straight update to the UI. 我遇到的每个Vuex示例似乎只是处理UI的直接更新。 But in almost every case I need to also perform key component based operations. 但几乎在每种情况下,我都需要执行基于组件的关键操作。

state: {
  // initial state values are all falsey
  id: false,
  name: false,
},
getters:   {
  getItem: (state) => {
    return state;
  },
},
actions: {
  setItem({commit}) {
    // async call to get and then commit the state
  }
}

Above is the relevant snippet of the item store a very simple example where the call to dispatch the action is typically called from a component not detailed here. 上面是项目存储的相关片段,这是一个非常简单的示例,其中调度操作的调用通常是从此处未详述的组件调用的。 Below is the component where I'm watching for the item to be populated. 下面是我正在观看要填充的项目的组件。

watch:    {
  item: function (newItem) {
    this.doSomethingWith(newItem);  // this never runs
  }
},
computed: {
  ...mapGetters({
    item: 'getItem',
  }),
},
mounted() {
  console.log(this.item);  // I get an observer of the item object

  // none of items state properties are defined during mounted
},
methods: {
 doSomethingWith(item) {
   // I want to do something here with the set item!!
 }
}

I expect the items state not to be set on mount as it's an async call to an external api. 我希望项目状态不要设置为mount,因为它是对外部api的异步调用。 However what I did expect is when it is eventually populated the watcher would catch that and allow me to run subsequent operations inside the component. 然而,我所期望的是,当它最终被填充时,观察者会抓住它并允许我在组件内运行后续操作。 However the watch doesn't ever fire. 然而,手表永远不会发射。

So how do I track changes like this in components and run operations dependant on their new state? 那么如何在组件中跟踪这样的更改并根据新状态运行操作?

The watch never fires because the code is watching the object returned from getItem , which is the state object and the reference to the state object doesn't change. 手表永远不会触发,因为代码是看从返回的对象getItem ,这是state对象和参照state的对象不会改变。 Only the state object's properties change. 只有状态对象的属性发生变化。 If you want to fire the watch, you need to perform a deep watch. 如果你想要发射手表,你需要进行deep观察。

watch:    {
  item: {
    handler:function (newItem) {
      this.doSomethingWith(newItem);
    },
    deep:true
  }
},

Depending on your application this might not be very performant. 根据您的应用程序,这可能不是非常高效。 You might want to use a getter that watches a specific property of the state. 您可能希望使用监视状态的特定属性的getter。

Or, return a promise from an action that kicks off the asynchronous API call and do what you need to in a success handler. 或者,从一个启动异步API调用的操作返回一个promise,并在成功处理程序中执行您需要的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM