首页 > 解决方案 > Vue - 在运行功能之前检查数据已设置

问题描述

data调用函数时,我对更新所用的时间有疑问。

In the example below, when an option is selected, the plugin sets the element 'inputs.example' (I can see this on in the Vue Tools) to the option picked, when the option is picked it triggers a function ( triggerFunction()), however似乎该功能在应用程序有时间设置数据之前被触发。结果,我得到了“空”数据,即使它似乎可用。

如何确保在函数运行之前设置数据?

谢谢。

落下

<Multiselect
 v-model="inputs.example"
 :options="options"
 @select="triggerFunction(options.vendorOptions)">
 </Multiselect>

功能

triggerFunction() {
      if (this.inputs.example != null) {
        fetch(`${API_URL}api/v1/endpoint/${this.inputs.example._id}`, {
            method: "GET",
            headers: {
              authorization: `Bearer ${localStorage.token}`
            }
          })
          .then(res => res.json())
          .then(output. => {
            console.log(output);
          });
      } else {
        console.log("No Option Selected";
      }
    }

标签: vue.jsvuejs2

解决方案


推荐阅读