ktsn / vuex-smart-module

Type safe Vuex module with powerful module features
MIT License
381 stars 19 forks source link

No $store in components #526

Open xornet-sl opened 2 years ago

xornet-sl commented 2 years ago

I'm using version 1.0.0 with vue 3 and vuex 4. TS 4.1.6 When I'm direct-importing created store and trying to use it just like in documentation - everything is totally fine. But I want to register it in all components and use my store in methods using this.$store. When I try to do that I don't see any $store registered (but store was used by createApp().use(store) in index.ts). I don't know why but this code from global.d.ts doesn't work automatically:

declare module "@vue/runtime-core" {
  interface ComponentCustomProperties {
    $store: Store<unknown>;
  }
}

And even if I add this code manually in my store's index.ts, I don't get any typing in this.$store just because $store is Store<unknown>

image

createComposable works just fine but I don't like to use setup() everywhere...

vdkrasny commented 2 years ago

This library does not provide typing of the global $store option. In order to get your module with all types you either have to use Component Mapper or Composable Function or context

import Vue from 'vue'
import { foo } from '@/store/modules/foo'

export default Vue.extend({
  mounted() {
    const typedFooModule = foo.context(this.$store)

    // Call `increment` action
    typedFooModule.actions.increment(1)
  }
})