【问题标题】:axios call in a method with vuejs and nuxtaxios 调用带有 vuejs 和 nuxt 的方法
【发布时间】:2019-01-09 23:57:35
【问题描述】:

对于我的国家/地区列表中的每个国家/地区,我需要使用 axios 进行 api 调用以获得另一个值,这里是 y 组件:

<template>
  <div>
    <div v-for="(country, i) in countries" :key="i">
      <div>{{ county[i.id].count }}</div>
    </div>
  </div>
</template>

在我的脚本中,我在 mount 上调用我的方法 matchCount 并将值存储在我的县数据对象中:

<script>
export default {
  props: {
   countries: {
    type: Array,
    required: true
    }
  },
 data() {
  return {
    county = {}
  };
 },
 mounted() {
  this.matchCount();
 },
 methods: {
   matchCount() {
     var paysCount = this.pays;
     paysCount.forEach(item => {
       this.$axios
        .get(
          `https://api.com/federation/}/${item.id}/`
        )
        .then(response => {
          this.county[item.id] = {};
          this.county[item.id].count = response.data.length.toString();
        });
     });
   }
  }
};
</script>

我收到这个错误“TypeError: Cannot read property 'count' of undefined”,我应该如何调用这个方法?

【问题讨论】:

    标签: vue.js axios nuxt.js


    【解决方案1】:

    您会发现在您的 HTML 模板中使用以下语法很有用 {{variable[key] &amp;&amp; variable[key].value}}

    在您的特定情况下,它将是:

    <template>
      <div>
        <div v-for="(country, i) in countries" :key="i">
          <div>{{ county[i.id] && county[i.id].count }}</div>
        </div>
      </div>
    </template>
    

    它的作用本质上是验证i.id 是否存在于county 数组中。如果没有,它不会抛出关于丢失对象/键的错误。

    您也可以在使用对象时使用此语法,如下所示:

    <div v-text="house.dog && house.dog.name" ></div>
    

    如果doghouse 对象中,那么它将显示dog's 名称。

    编辑:

    在函数中添加this.$forceUpdate();

    matchCount() {
     var paysCount = this.pays;
     paysCount.forEach(item => {
       this.$axios
        .get(
          `https://api.com/federation/}/${item.id}/`
        )
        .then(response => {
          this.county[item.id] = {};
          this.county[item.id].count = response.data.length.toString();
          this.$forceUpdate();
        });
     });
    }
    

    【讨论】:

    • 它在第一次渲染中不起作用,我的计数属性只有在我的组件发生变化时才会出现。
    • 似乎我从未听说过...我要去挖掘文档,太酷了!
    • 一个小建议:如果你对你的服务器进行如此多的调用,它会影响你的网页的性能。尝试一次通话(如果可以的话),然后拨打forceUpdate。对于较小的应用程序很好,但对于大型应用程序则不然。 + this.$forceUpdate 将更新 Vue 根目录中的所有内容,并且它的组件在循环的每次迭代中都需要一些时间。
    • 你的建议实际上似乎很有必要,所以我会尝试以另一种方式做到这一点。
    【解决方案2】:

    county[item.id].count 是异步设置的,在渲染组件时可能不可用。您可以添加安全检查:

    <template>
      <div>
        <div v-for="(country, i) in countries" :key="i">
          <div v-if="county[i.id]">{{ county[i.id].count }}</div>
          <div v-else>Loading...</div>
        </div>
      </div>
    </template>
    

    你好像有reactivity problem:

    this.$axios
        .get(
          `https://api.com/federation/}/${item.id}/`
        )
        .then(response => {
          this.$set(this.county, item.id, {count: response.data.length.toString())
        });
    

    【讨论】:

    • 我试过了,但在这种情况下什么都没有出现,但是我的县对象充满了数据。
    • 您的反应似乎有问题。我已经更新了答案。请试一试!
    猜你喜欢
    • 2020-12-02
    • 2018-04-23
    • 2018-03-05
    • 2021-07-02
    • 2021-12-06
    • 2019-10-26
    • 2020-05-02
    • 1970-01-01
    • 2020-05-26
    相关资源
    最近更新 更多