【问题标题】:VueJS does not update DOM after fetch data from API?从 API 获取数据后,VueJS 不更新 DOM?
【发布时间】:2019-01-18 18:26:07
【问题描述】:

我正在尝试创建一个关于照片列表的示例,但在调用 API 后将数据绑定到组件时遇到了问题。

JS代码:

<script>
// photo item
Vue.component('photo-item', {
   props: ['photo'],
   template: `<li>{{ photo.name }}</li>`
});

// List of photos
Vue.component('photo-list', {
   props: ['photos'],

   template: `
   <ul id="photo-list">
      <photo-item v-for="photo in photos" :photo="photo"></photo-item>
   </ul>`
});

new Vue({
   el: "#photo_detail",
   data: {
      photos: []
   },

   created: function() {
      axios
       .get('/api/photos')
       .then(function (response) {
           this.photos = response.data; // Data existed
       })
       .catch(function (err) {
           console.log(err);
       });
   }
 })
 </script>

HTML 代码

<main id="photo_detail">
    <photo-list v-for="photo in photos" :photo="photo"></photo-list>
</main>

从 API 获取所有照片后,据我了解,变量 photos 将自动绑定,VueJS 将更新 DOM。

VueJs 2.1.6

任何帮助。

谢谢!

【问题讨论】:

    标签: vue.js vuejs2 vue-component


    【解决方案1】:

    您的代码不正确。

    问题:

    1. 最好为每个组件定义使用的组件,例如 components: {photoItem}。
    2. 在您的 axios 回调中,您使用了function,这意味着您在内部使用了错误的上下文 (this.photos)。使用箭头功能 (() =&gt; {}) 而不是 function () {}
    3. 指令v-for 需要指令:key=""

    我已经在下面修复了。

    // photo item
    const photoItem = Vue.component('photo-item', {
       props: ['photo'],
       template: `<li>{{ photo.name }}</li>`
    });
    
    // List of photos
    const photoList = Vue.component('photo-list', {
    
      // define used components
      components: {photoItem},
      props: ['photos'],
      template: `
       <ul id="photo-list">
    
          <!-- add :key="" directive -->
          <photo-item v-for="(photo, index) in photos" :key="index" :photo="photo"></photo-item>
       </ul>`
    });
    
    
    new Vue({
       el: "#photo_detail",
    
       // define used components
       components: {photoList},
       data: {
          photos: []
       },
    
       created: function() {
    
          // axios.get('/api/photos')
          // use arrow function
          setTimeout(() => {
    		this.photos = [{name: 'Photo 1'}, {name: 'Photo 2'}];
          }, 1000);
       }
     })
    <script src="https://unpkg.com/vue"></script>
    
    <main id="photo_detail">
      <photo-list :photos="photos"></photo-list>
    </main>

    【讨论】:

    • 为我工作:) 非常感谢 :]
    【解决方案2】:

    问题在于 function() 内的 this 值,该值的范围为 axios 而不是 vue instance 。 或者你可以使用(response)=&gt;直接使用this

    new Vue({
       el: "#photo_detail",
       data: {
          photos: []
       },
    
       created: function() {
          var self=this;
          axios
           .get('/api/photos')
           .then(function (response) {
               self.photos = response.data; // Data existed
           })
           .catch(function (err) {
               console.log(err);
           });
       }
     })
    

    【讨论】:

    • Ahhhhhh...是的,太好了,太基础了:D,谢谢@Helping,关闭我的问题:D
    • 很高兴它有帮助 :) 我自己在这个简单的问题上花了好几个小时:D
    • @Helpinghand 非常感谢!
    • 它有效,但出现此错误:Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value.
    猜你喜欢
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 2018-01-13
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多