javascript - Vue 2.0 : Passing asynchronous data to child component

标签 javascript asynchronous vue.js vuejs2

我有一个父 Vue 组件,它通过 prop 将数据传递给它的子组件,但数据是异步可用的,因此我的子组件初始化为未定义的值。

在数据可用之前,我该怎么做才能阻止初始化?

父级:

  var employees = new Vue({
    el: '#employees',
    data: { ... },
    methods: {
      fetch: function(model, args=null) {

      let url = "/" + model + ".json"
      console.log(url);
      $.ajax({
        url: url,
        success: ((res) => {
          console.log(res)
          this[model] = res;
          this.isLoading = false;
        error: (() =>  {
          this.isLoading = false;
        }),
        complete: (() => {
          // $('.loading').hide();
          this.isLoading = false;
        })
      })

    },
    mounted: function() {
      this.fetch(...)
      this.fetch(...)
      this.fetch('appointments')
    }
  })

我的 fetch 方法被调用了多次。

最佳答案

你可以在父模板中使用v-if:

<template v-if="everthingIsReady">
    <child-item :data="data"></child-item>
</template>

everythingIsReady 设置为 true 之前,不会创建子项,您可以在所有调用完成后立即设置它。

关于javascript - Vue 2.0 : Passing asynchronous data to child component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45059832/

相关文章:

javascript - 我的 subnav 上的填充错误

javascript - 使用 JavaScript - 如何计算 Microsoft Word 文档中的字数?

asynchronous - Flutter Future<dynamic> 与 Future<String> 子类型错误?

C# 异步网络 IO 和 OutOfMemoryException

javascript - 在vuejs中引用router-link参数数据

javascript - SyntaxError : JSON. parse : unexpected character at line 1 column 1 of the JSON data, 为什么?

javascript - 注入(inject) HTML5 数据属性与注入(inject) javascript 数据结构

c# - EF 事务和异步

javascript - 如何在表格行元素中绑定(bind)多个CSS类?

javascript - 将 props 传递给 Vue 组件中的元素属性