Usage in Vue2 with TypeScript #1204
Unanswered
Radiergummi
asked this question in
Help and Questions
Replies: 3 comments 11 replies
-
What IDE are you using? |
Beta Was this translation helpful? Give feedback.
1 reply
-
You cannot destructure state like this const { counter, increment } = useCounterStore(); It breaks reactivity, like with const counterStore = useCounterStore()
// like with reactive, you need to extract refs
const { counter } = storeToRefs(counterStore)
// actions can be destructured
const { increment } = counterStore |
Beta Was this translation helpful? Give feedback.
4 replies
-
Update: I've switched to using |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey there! I'm working on migrating our (pretty big) Nuxt/TS/Vuex application to a Vue2/TS/Vite/Pinia stack. As we're locked in to Vuetify, I can't migrate to Vue3 yet (which is a shame, but... ¯\_(ツ)_/¯).
As we're inevitably going to migrate to Vue3 later on, I don't want to make this harder than it has to be and opted out of class-style components; we're basically just using standard Vue2 SFC components.
After following the getting started documentation on state usage, I noticed none of the provided examples actually work completely: Either Vue complains, or TypeScript complains, or my IDE isn't able to provide completion.
What I've tried, given the following test store:
Using a
setup
hookSetting the store in the
setup
hook and accessing thatthis.store
:Using
mapState
andmapActions
This is accepted by TypeScript, the increment action works, but the IDE is unable to complete the property and the action and reports them as undefined.
Calling the store in computed or methods individually
This provides working completions, Typescript accepts it, and the increment action works, but it feels wrong to call the store multiple times and I don't understand the performance implications of that.
This feels a little unsatisfying; I would expect the examples from the documentation to work without hassles like these. What is the actual, correct way to use Pinia in Vue2 with Typescript? I would prefer the first or second options using a
setup
hook (especially to make migrating to Vue3 easier later on), but that doesn't work or causes Typescript to trip over it.What can I do to make the code work as advertised in the documentation?
(I can cobble up a repro repository if needed of course)
Beta Was this translation helpful? Give feedback.
All reactions