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
Change
src/stores/cart
to@/src/stores/cart
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:
Also, if possible, mark the ‘src’ folder as a source folder in your IDE.