I recently upgraded to Vue 3 and I'm trying to populate a reactive object by fetching data from a JSON API.
Here's how my component looks. Surprisingly, I am not encountering any errors but the expected results are not showing up either.
<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>
The expectation was to display a number on the screen as per the total
field in the JSON data.
Concerning state.test
When I tried outputting just state.test
, this is what I got:
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); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }
Do you have any suggestions on how to overcome this hurdle?