【发布时间】:2021-02-07 08:46:38
【问题描述】:
我已经安装了 Vue 3,并希望使用从 JSON API 获取的数据来填充反应式对象。
我的组件如下所示。我没有收到任何错误,但我也没有得到任何结果。
<template>
<div>
{{ state.test.total }}
</div>
</template>
<script>
import { reactive } from "vue";
export default {
setup() {
const state = reactive({
test: null,
});
state.test = async () => {
return await fetch("https://api.npms.io/v2/search?q=vue");
};
return {
state,
};
},
};
</script>
我希望在屏幕上显示一个数字,因为那是 JSON 文件中 total 中的内容。
开启state.test
如果我只输出state.test,我会得到下面的输出。
function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } 函数_throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }
知道如何绕过它吗?
【问题讨论】:
标签: javascript vue.js async-await fetch vuejs3