TypeError: Cannot read properties of undefined (reading '$pinia') #2257
-
main.ts import { createApp } from 'vue'
import App from './App.vue'
import { createPinia } from 'pinia'
const store = createPinia()
const app = createApp(App)
app.use(store)
app.mount('#app') store.ts import { defineStore } from 'pinia'
import { User } from '@/types/user.ts'
type AppStoreState = {
users: User[]
}
type AppStoreGetter = {}
type AppStoreAction = {
fetchUsers(): void
}
export const useAppStore = defineStore<
'app',
AppStoreState,
AppStoreGetter,
AppStoreAction
>({
id: 'app',
state: () => ({
users: []
}),
actions: {
fetchUsers() {
// TODO
},
},
}) App.vue <script setup lang="ts">
import { onMounted } from 'vue'
import { useAppStore } from '.store.ts'
import { mapActions } from 'pinia'
const { fetchUsers } = mapActions(useAppStore, {
fetchUsers: 'fetchUsers',
})
onMounted(() => {
fetchUsers()
})
</script>
<template>
<-- TODO -->
</template> |
Beta Was this translation helpful? Give feedback.
Answered by
posva
Jun 13, 2023
Replies: 1 comment
-
BTW don't pass the generics to |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
liyuan1125
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
mapActions()
are for options API. Check https://pinia.vuejs.org/core-concepts/#using-the-store, you just need to calluseAppStore()
instead.BTW don't pass the generics to
defineStore()
, pinia infers the types for you 😉