如何在应用程序首次加载时在Vue中加载存储

huangapple go评论97阅读模式
英文:

How to load the store in vue when the app first loads

问题

我想在应用程序加载时调用getProducts。我能够在Vue2中实现这一点,但不确定如何在Vue的新组合API版本中实现。请问有人可以建议我如何做到这一点吗?

英文:

I am trying to load products from my pinia store when my Vue app first loads.

This is my app.js

  1. import {createApp} from "vue";
  2. import App from "./App.vue";
  3. import router from "./Router/index";
  4. import { createPinia } from "pinia";
  5. createApp(App)
  6. .use(router)
  7. .use(createPinia())
  8. .mount("#app")

And this is my store:

  1. import { defineStore } from "pinia";
  2. import axios from "axios";
  3. const state = {
  4. cart: [],
  5. order: {},
  6. customer: {},
  7. loading: true,
  8. error: null,
  9. products: [],
  10. };
  11. export const useCartStore = defineStore("shopState", {
  12. state: () => state,
  13. actions: {
  14. async getProducts() {
  15. try {
  16. this.loading = true;
  17. const response = await axios.get("/api/product");
  18. this.products = response.data.data;
  19. this.loading = false;
  20. } catch (error) {
  21. this.error = error;
  22. }
  23. },
  24. addToCart({ item }) {
  25. const foundProductInCartIndex = this.cart.findIndex(
  26. (cartItem) => item.slug === cartItem.slug
  27. );
  28. if (foundProductInCartIndex > -1) {
  29. this.cart[foundProductInCartIndex].quantity += 1;
  30. } else {
  31. item.quantity = 1;
  32. this.cart.push(item);
  33. }
  34. },
  35. removeProductFromCart({ item }) {
  36. this.cart.splice(this.cart.indexOf(item), 1);
  37. },
  38. clearCart() {
  39. this.cart.length = 0;
  40. },
  41. clearCustomer() {
  42. this.customer = {};
  43. },
  44. clearOrder() {
  45. this.order = {};
  46. },
  47. updateCustomer(customer) {
  48. this.customer = customer;
  49. },
  50. updateOrder(order) {
  51. this.order = order;
  52. },
  53. getSingleProduct(slug) {
  54. return this.products.find((product) => product.slug === slug);
  55. },
  56. },
  57. getters: {
  58. getCartQuantity() {
  59. return this.cart.reduce(
  60. (total, product) => total + product.quantity,
  61. 0
  62. );
  63. },
  64. getOrderDetails() {
  65. return this.order;
  66. },
  67. getCartContents() {
  68. return this.cart;
  69. },
  70. getCustomer() {
  71. return this.customer;
  72. },
  73. getCartTotal() {
  74. return this.cart.reduce(
  75. (total, product) => total + product.price * product.quantity,
  76. 0
  77. );
  78. },
  79. },
  80. persist: true,
  81. });

I would like to call getProducts when the app loads. I am able to do this using Vue2 but not sure how to do this with the new composition API version of Vue. Please can someone advise how I can do this?

答案1

得分: 1

如果你想在应用程序加载后加载产品,你可以在组合 API 中使用 onMounted() 钩子。

在你想加载产品的组件中:

  1. <script setup>
  2. import { onMounted } from 'vue';
  3. import { useCartStore } from '../stores/storeName.js';
  4. const store = useCartStore();
  5. onMounted(() => {
  6. store.getProducts();
  7. });
  8. </script>

注意:我在这里使用了 <script setup>。但如果你使用 setup() 钩子,你需要手动返回该函数。

参考链接:https://vuejs.org/api/composition-api-lifecycle.html#onmounted

如果你要加载产品,可以在应用程序加载后使用组合 API 中的 onMounted() 钩子。

英文:

If you want to load the products once the app is loaded, you can use the onMounted() hook in the composition API.

https://vuejs.org/api/composition-api-lifecycle.html#onmounted

On the component where you want to load the products:

  1. <script setup>
  2. import { onMounted } from 'vue';
  3. import { useCartStore } from '../stores/storeName.js';
  4. const store = useCartStore()
  5. onMounted(() => {
  6. store.getProducts()
  7. })
  8. </script>

Note: I'm using <script setup> here. But if you're using the setup() hook you need to manually return the function

https://vuejs.org/api/composition-api-setup.html

huangapple
  • 本文由 发表于 2023年2月19日 07:09:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定