简体   繁体   English

数据显示在控制台中但不在vue模板中

[英]data showing in console but not in vue template

I am trying to fetch some data with VUE from API that I created using Laravel Resource. 我正在尝试从使用Laravel Resource创建的API中使用VUE获取一些数据。 The array in data is populating and showing in console perfectly. 数据数组正在完美填充并显示在控制台中。 But when I am trying to show the data in template under a v-for loop with v-bind, it is not showing anything. 但是,当我尝试使用v-bind在v-for循环下显示模板中的数据时,它没有显示任何内容。 If I clear the cache in my browser, it is still not showing any data. 如果清除浏览器中的缓存,它仍然不会显示任何数据。 But if I clear the cache in my browser while being on another page and then come back to the page where the vue template is using back button, the data is there till I refresh. 但是,如果我在另一页上的同时清除浏览器中的缓存,然后返回到使用vue模板的“后退”按钮的页面,则数据一直存在,直到刷新为止。

Here is the code in template: 这是模板中的代码:

<template>
    <div>
        <h2>Feeds</h2>
        <div class="card card-body" >
          <ul>
            <li v-for="feed in feeds" v-bind:key="feed.id">
                <h3>{{feed.title}}</h3>
                <p>{{feed.body}}</p>
            </li>
          </ul>
        </div>
    </div>    
</template>

And here is my script: 这是我的脚本:

export default {
data(){
    return {
        feeds: []
    }
},
created(){
    this.fetchFeeds();
},
methods:{
    fetchFeeds(page_url){
        let vm = this;
        page_url= page_url ||'feeds/api'
        fetch(page_url)
        .then(res => res.json())
        .then(res =>{
            vm.feeds = res.data;
            console.log(vm.feeds);            
        })
        .catch(err => console.log(err));
    }
  }
}

It can be vue reactivity issue. 这可能是vue反应性问题。 Try using Object.assign as below 尝试如下使用Object.assign

 fetchFeeds(page_url){
    let vm = this;
    page_url= page_url ||'feeds/api'
    fetch(page_url)
    .then(res => res.json())
    .then(res =>{

        vm.feeds =  Object.assign({}, res.data);
        console.log(vm.feeds);            
    })
    .catch(err => console.log(err));
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM