Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there an equivalent to this.$store.watch or this.$store.subscribe? #115

Closed
terrymun opened this issue Apr 17, 2019 · 2 comments
Closed

Comments

@terrymun
Copy link

terrymun commented Apr 17, 2019

I am wondering if is possible to watch store properties, or subscribe to actions/mutations, inside a VueJS component, when using vuex-module-decorators?

Of course, I can always do this, assuming that someStore is assigned with getModule(<storeClassName>). The code below is an example code from inside a VueJS component using vue-property-decorator:

// Setup internal property
private get someStoreGetter() {
  return someStore.property;
}

// Watch changes to internal property
@Watch
private onSomeStoreGetterChange() {
  console.log(someStore.property);
}

However, I find this rather cumbersome and kind of a code smell, because it creates an unnecessary property on the VueJS component for the sole purpose of watching changes to it.

@litao10422
Copy link

I'd also like to know the best solution to this question.

@darylteo
Copy link

darylteo commented Aug 5, 2019

I have done this

PublishingListStore.ts

import {Action, getModule, Module, Mutation, MutationAction, VuexModule} from "vuex-module-decorators";

import {store} from "src/store";

@Module({
    dynamic: true,
    store: store,
    stateFactory: true,
    name: 'publishing',
    namespaced: true,
})
export default class PublishingListStore extends VuexModule {
       public tab: number = 0;
       ...
}

store.watch((_) => getModule(PublishingListStore, store).tab, (value: number, oldValue: number) => {
    console.log("tab changed", value, oldValue);
});

store.watch((state: any) => state.publishing.tab, (value: number, oldValue: number) => {
    console.log("tab changed", value, oldValue);
});

Both should be equivalent... but I'm not 100% sure on whether this requires the store to be registered to the store, but getModule should cause the store to dynamically register itself.

The only way to do this on dynamic registration is to modify getModule itself to do it after calling registerModule perhaps. michaelolof/vuex-class-component#27 has a interesting API proposal that will do just that using statics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants