skip to Main Content

I am trying to use pinia state management in vue 3 but I am getting this error

Module not found: Error: Can’t resolve ‘src/stores/cart’ in
‘C:UsersAli Haidertheme-projectsrcviewspages

// This is my Products component
<script>
import { defineComponent } from "vue";
import { useCartStore } from "src/stores/cart"; // Import your cart store

export default defineComponent({
  name: "Products-",
  setup() {
    const cartStore = useCartStore(); // Use your cart store

    const addToCart = (product) => {
      cartStore.addToCart(product);
    };

    return {
      addToCart,
    };
  },
});
</script>

Below is my Stores/cart.js file in which I have declared pinia state and where I am trying to use it in the products. What am I doing wrong? Also how can I use pinia with the laravel API to fetch data?

import { defineStore } from "pinia";

export const useCartStore = defineStore("cart", {
  state: () => ({
    cart: [],
  }),
  actions: {
    addToCart(product) {
      console.log("product added");
      this.cart.push(product);
    },
  },
});

2

Answers


  1. Change src/stores/cart to @/src/stores/cart

    Login or Signup to reply.
  2. If you’re using an IDE like IntelliJ, the linter will help you see if the reference is good or not.

    As mentioned by Estus Flask, you might try using dots in your path. My imports usually look like:

    import { useCartStore } from "./src/stores/cart";
    

    Also, if possible, mark the ‘src’ folder as a source folder in your IDE.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search