小编yer*_*rin的帖子

如何在 Vue.js 3 中使用 Pinia 正确处理从 API 获取的信息?

我目前正在开发一个从 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});\n
Run 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)

api vue.js vuejs3 pinia

10
推荐指数
1
解决办法
2万
查看次数

标签 统计

api ×1

pinia ×1

vue.js ×1

vuejs3 ×1