ktsn / vuex-class

Binding helpers for Vuex and vue-class-component
MIT License
1.73k stars 86 forks source link

"Subscribe" to @Getter with parameters #55

Closed sidyes closed 5 years ago

sidyes commented 5 years ago

Hi folks!

I still got one question regarding parameterized Getters. At the moment I have a parametrized getter like this in my component:

....
@Getter("getItemByUser", { namespace: "items" })
  getItemByUser!: (user: User) => Item;

public item: Item;

public created(): void {
 this.item = this.getItemByUser(this.user);
}
...

The problem now is that the item property does not get updated when something in the state (which is used by the getItemByUser getter) changes. So it just gets called once and that's it. How can I somehow "subscribe" to this? A property-style access getter automatically updates it value when something changes.

Can you help me out there?

relmithad commented 5 years ago

Just define a component getter within your class:

@Getter("getItemByUser", { namespace: "items" })
  getItemByUser!: (user: User) => Item;

// public item: Item;

// public created(): void {
// this.item = this.getItemByUser(this.user);
//}

get item(): Item  {
 return this.getItemByUser(this.user);
}