Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
accessing a pinia store in multiple components

this is my first time using pinia and I am trying to implement a shoppingcart store. I have a page where I add items to my shoppingcart store and another where I want to display them, however when I navigate to the shoppingcart page (where I want to display them) my state is completely empty. It was my understanding of pinia that my state would carry over from one page to another. I am using vue3

My store definition

import { defineStore } from "pinia";

export const shoppingCartStore = defineStore({
  id: "shoppingCart",
  state: () => ({
    products: {},
    productData: {},
    totalPrice: 0.0,
    numberOfProducts: 0
  }),
  actions: ... (to add and remove items from the store)
    addProduct(product,amount) {
      if (product.priceid in this.products){
        this.products[product.priceid] += amount
      }
      else{
        this.products[product.priceid] = amount
        this.productData[product.priceid] = product
      }
    }
  },
});

The page where I add items to my shoppingcart:

import { shoppingCartStore } from '@/stores/shoppingCart.js'
export default {
    setup(){
        const shoppingCart = shoppingCartStore()
        return { shoppingCart }
    },
...
// this is how I add items to my shoppingcart (it is in a function I just left all the other code out for simplicity
this.shoppingCart.addProduct(product,1) 
...

The page where I want to display my shoppingcart:

import { shoppingCartStore } from '@/stores/shoppingCart.js'

export default {
    setup(){
        const shoppingCart = shoppingCartStore()
        return { shoppingCart }
    },

main.js (this is how I instantiate Pinia)

...
let pinia = createPinia();

app.use(pinia);
...

When I was looking this error up I found this github discussion where someone explains that it should work like I am using it but for me it doesn't.

Thanks in advance for any help

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda