【问题标题】:Getting data only after click in Vuejs只有在 Vuejs 中点击后才能获取数据
【发布时间】:2018-07-18 10:52:11
【问题描述】:

大家下午好。感谢您阅读本文。我正在尝试填充项目列表。 objects1 是一个包含多个数据的项目数组,包括用于获取项目图片的 url。问题是我只看到空白而不是更新的图片。当我单击“返回”(转到上一页的按钮)时,我能够看到所有图片一秒钟 在显示上一页之前。

 <template>
   <div v-for="item in itemList">
    <div class="item-picture" :style="{ 'backgroundImage' : 'url(' + 
      item.picture + ')' }"></div>
      <p>{{item.name}}</p>
      <p>{{item.address}}</p>
    </div>
  </div>
 </template>

<script>
   import axios from 'axios'

   export default {
   name: ‘name1’,
   props: [
      ‘objects1’
   ],
   data () {
     return {
        objects2: [],
     }
   },
   watch: {
    objects1: function () {
      this.objects2 = this.objects1
      for (let i = 0; i < this.objects2.length; i++) {
        this.getPic(this.objects2[i].url, i)
      }
    },
  },
  mounted () {
  },
  updated () {
  },
  methods: {
    getPic (url, index) {
      let _this = this
      axios.get(url)
      .then((res) => {
          _this.objects2[index].picture = res.data.url
        }
      })
      .catch((error) => {
      })
    },
  }
}

【问题讨论】:

    标签: api vue.js axios


    【解决方案1】:

    尝试使用 Vue.set 并查看文档中的更改检测警告 (https://vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats)

    代替

    _this.objects2[index].picture = res.data.url
    

    使用

    Vue.set(this.objects2[index], 'picture', res.data.url)
    

    【讨论】:

    • 感谢您的快速回复。有用。我会阅读那个链接。
    猜你喜欢
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 2021-07-05
    • 2015-10-29
    相关资源
    最近更新 更多