我想遍历包含对象的数组,并从这些对象中获取作者姓名列表。
为此,我想编写一个我将forEach()通过的方法。但是当我console.log(articles)或其console.log(this.newsFeed)返回undefined或只是空白时,我似乎无法访问包含该数组的计算属性。
显然我在做错事,但我不明白...
这是我的代码:
new Vue({
el: '#feed',
data: {
newsFeed: "",
},
created: function() {
console.log('running');
this.getData();
this.filterAuthors();
},
computed:
{
articles: function() {
var articles = this.newsFeed.articles;
return articles;
},
},
methods:
{
getData: function() {
var newsFeed = this.$http.get('https://newsapi.org/v1/articles?source=the-next-web&sortby=latest&apikey='+ apikey).then(response => {
this.newsFeed = response.body;
}, response => {
console.error(error);
});
},
filterAuthors: function() {
var articles = this.newsFeed.articles;
var authorsArray = [];
console.log(articles);
// commented out while troubleshooting …Run Code Online (Sandbox Code Playgroud)