【问题标题】:Vue.js i cant display fetched dataVue.js 我无法显示获取的数据
【发布时间】:2019-06-10 14:48:40
【问题描述】:

我正在使用 fetch 获取提要,但不知何故我的 vue 应用程序没有呈现它我尝试了不同的东西,但似乎没有任何东西适合我。

<section id="vue-instagram" class="instagram">
  <div class="instagram__col"  v-for="item in feed">
    <div class="instagram__col__picturebox">
      <img v-if="item.media_url.indexOf('.mp4') == -1" v-bind:src="item.media_url"/>
      <video v-if="item.media_url.indexOf('.mp4') > 0" loop preload muted controls style="width:100%;">
        <source v-bind:src="item.media_url" />
      </video>
    </div>
    <div class="instagram__col__description">
      <div class="instagram__col__descroption--icon">
        <img src="images/insta.png"/>
      </div>
      <div class="instagram__col__descroption--text">
        {{item.caption.substring(0,90)}}...
        <div class="hashtags bold-text">
          {{item.caption.substring(item.caption.indexOf('#'), item.caption.length).split(' ').slice(0,5).join(' ')}}
        </div>
      </div>
    </div>
  </div>
</section>

var app = new Vue({
  el: '#vue-instagram',  
  data:() => ({
    loading: false,
    feed: [],
  }),
}, created: async () => {
  var response = await fetch("URL_TOSERVER", {
    method: "POST",
  });
  var {data} = await response.json();
  this.feed = data; 
  console.log(data);
});

如果我在 chrome 中调用 this.feed,它会显示数据,但如果我深入到 app.data 它是空的。

【问题讨论】:

标签: vue.js vuejs2


【解决方案1】:

解决方案是将调用提取为方法

new Vue({
  el: '#vue-instagram',  
  data: {   
    loading: false,
    feed: [], 
  },
  created: function() {
    this.fetchData();
  },
  methods: {
    fetchData: function(){
      var self = this;  
      fetch("http://727.dk.web81.curanetserver.dk/umbraco/api/instagram/index", {
        method:"POST"
      }).then(function(response){
        return response.json();
      }).then(function(data){
        self.feed = data.data; 
      });
    }
  },
  computed: {
    posts: function() {
      return this.feed;
    }
  } 
});

【讨论】:

  • 实际的问题是你使用的生命周期钩子的箭头函数没有提供正确的this
猜你喜欢
  • 2021-01-16
  • 1970-01-01
  • 2019-12-11
  • 1970-01-01
  • 2019-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-25
相关资源
最近更新 更多