我目前正在开发一个从 API 获取数据的项目。\n我需要访问我的应用程序中的所有数据,所以我认为最好的选择是使用 Pinia(通常我使用 Vuex,但我想尝试一下)这个“新”商店解决方案)。
\n我的“问题”是,我真的不知道我实现目标的方式是否是最好的方式,甚至是“良好的做法”。在我的 Pinia 商店里我写了这样的内容:
\n\nexport const listadoADPs = defineStore("listado", {\n state: () => ({\n adps: [],\n }),\n actions: {\n getADPs() {\n const api =\n "URL";\n\n fetch(api)\n .then((response) => response.json())\n .then(({ data }) => (this.adps = data))\n .catch((error) => console.log(error));\n },\n },\n});\nRun Code Online (Sandbox Code Playgroud)\n然后,在我的组件中我编写了以下代码:
\n<script setup>\nimport { ref, computed } from "vue";\nimport { listadoADPs } from "@/stores/adps";\nconst store = listadoADPs();\n\nconst cards = ref([\n {\n number: computed(() => store.adps.length),\n description: "ADPs vigentes",\n },\n {\n number: …Run Code Online (Sandbox Code Playgroud)