Gos*_*ith 5 javascript destructuring chart.js
我正在尝试为chart.js提取数字的许可证数组
API报告数据的形状为:
{
"report": {
"usage": {
"chartLabels": [
"'1-Mar', '2-Mar', '3-Mar', '4-Mar', '5-Mar', '6-Mar', '7-Mar', '8-Mar', '9-Mar', '10-Mar', '11-Mar', '12-Mar', '13-Mar', '14-Mar', '15-Mar', '16-Mar', '17-Mar', '18-Mar', '19-Mar', '20-Mar', '21-Mar', '22-Mar', '23-Mar', '24-Mar', '25-Mar', '26-Mar', '27-Mar', '28-Mar', '29-Mar', '30-Mar', '31-Mar'"
],
"license": [
"'3', '50', '56', '53', '60', '56', '47', '3', '39', '67', '60', '57', '61', '61', '8', '47', '49', '51', '49', '45', '42', '3', '3', '4', '4', '3', '3', '3', '3', '3', '4'"
],
}
}
}
Run Code Online (Sandbox Code Playgroud)
是否可以通过fetch进行解构?我没有用console.log(license)找回任何东西
async mounted () {
this.loaded = false
try {
const { report: {usage: { license } } } = await fetch("http://localhost:8000/api/report/" + this.$route.params.id)
this.chartData = license
this.loaded = true
} catch (e) {
console.error(e)
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
提取返回响应
要进入json,您需要等待response.json()
像这样
async mounted() {
this.loaded = false
try {
const response = await fetch("http://localhost:8000/api/report/" + this.$route.params.id)
const {report: {usage: {license}}} = await response.json();
this.chartData = license
this.loaded = true
} catch (e) {
console.error(e)
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的最后一个答案,并在有效的代码段中将此答案与
async mounted() {
this.loaded = false
try {
const response = await fetch("http://localhost:8000/api/report/" + this.$route.params.id)
const {report: {usage: {license}}} = await response.json();
this.chartData = license
this.loaded = true
} catch (e) {
console.error(e)
}
}
Run Code Online (Sandbox Code Playgroud)