Vue3/Pinia: Using Pinia store inside a composable throws TypeError within pinia core code

103 views Asked by At

We are running a SPA compiled with vite and Vue3. As soon as we call the useStore() function inside a composable, we encounter a Type Error in the pinia code:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '_s')

It happens in the pinia core code

We can use this store without issues anywhere else in our app (SPA). It seems that the composables are run too early and create this error because the pinia instance is not ready yet (similar issue here, albeit with SSG: https://github.com/antfu/vite-ssg/issues/103 )

  1. Create a dummy store import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { Ref } from 'vue'

export const useTestStore = defineStore('customer-portal', () => {
  console.log('Defining Teststore...')
  const test: Ref<string> = ref('')
  return {
    test,
  }
})
  1. Create a composable using this store:
import { ref } from 'vue'
import { useTestStore } from '@/vue/store/test'
const store = useTestStore()
  1. Use theat composable in a component (import store and call useTestStore())
0

There are 0 answers