gpt4 book ai didi

javascript - 如何使用 Vue 和 Fetch Api 获取 json

转载 作者:行者123 更新时间:2023-11-30 19:47:04 25 4
gpt4 key购买 nike

我有一个基于 Vue.js 的简单代码:

const app = new Vue({
el: 'vue-app',
data: {
displayedBooks: {}
},
created() {
fetch('/library').then(response => response.json())
.then((data) => this.data.displayedBooks = data);
}
});

但是我有一个异常(exception):

Uncaught (in promise) TypeError: Cannot set property 'displayedBooks' of undefined at fetch.then.then (main.js:8)

为什么这个简单的代码不能正常工作?

最佳答案

它只是this.displayedBooks,而不是this.data.displayedBooks。 Vue 数据参数中的所有内容都直接附加到此。

关于javascript - 如何使用 Vue 和 Fetch Api 获取 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54845129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com