简体   繁体   中英

Pinia Store with Vue

I am getting an error on my component. Can someone help to solve this? I'm work in vue 3 version. Getting an error: "Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'counter')" Does anyone have any insights?

This id my code

Pinia Store:

import { ref, computed } from "vue"
import { defineStore } from "pinia"

export const useCounterStore = defineStore("counter", {
  state: () => {
    return {
      counter: 0
    }
  }
})

script:

<script setup>
  import { computed, ref } from "@vue/reactivity"
  import { useCounterStore } from "@/stores/counter"

  const storeCounter = useCounterStore()
  console.log(storeCounter)

  console.log(storeCounter.store.counter)
  console.log(storeCounter.counter)
</script>

and template:

<template>
  <div class="home">
    <div class="count">{{storeCounter.counter}}</div>
  </div>
</template>

One of these console.log statements works, the other does not

console.log(storeCounter.store.counter); // bad syntax, no store object on storeCounter
console.log(storeCounter.counter); // correct

All you have to do is delete the offending line. Simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM