学习VueJS并尝试对组件加载进行简单的API调用,以将回购列表放到我的页面上。当我拨打created()
方法并设置this.repos
时,没有问题。但是,如果我将它设置为一种方法,然后从this.getRepos
调用它,什么都不会发生。没有错误,没有。我错过了什么VueJS?为什么VueJS不会调用created()函数中的方法?
这工作:
data:() => ({
msg: 'Github Repos',
ok: 'Im practically giving away these repos',
repos: [],
}),
methods: {
},
async created() {
const repos = await axios.get('https://api.github.com/orgs/octokit/repos');
this.repos = repos.data.map(repo =>
`<div class="box"><a href="${repo.html_url}">
${repo.name}
</div>`,
);
},
这不起作用:
data:() => ({
msg: 'Github Repos',
ok: 'Im practically giving away these repos',
repos: [],
}),
methods: {
getRepos: async() => {
const repos = await axios.get('https://api.github.com/orgs/octokit/repos');
this.repos = repos.data.map(repo =>
`<div class="box"><a href="${repo.html_url}">
${repo.name}
</div>`,
);
},
},
created() {
this.getRepos();
},
任何想法?谢谢!
可能的复制[VueJS的:为什么 “这个” 不确定? ](https://stackoverflow.com/questions/43929650/vuejs-why-is-this-undefined) – Bert
只需 '异步getRepos(){' – Reiner