davestewart / vue-class-store

Universal Vue stores you write once and use anywhere
https://github.com/davestewart/vue-class-store-demos
MIT License
277 stars 13 forks source link

Accessing getters and methods externally #15

Closed Rigidoont closed 3 years ago

Rigidoont commented 3 years ago

Hi! Can getters and methods of a store be accessed from the outside? For example, from a component that uses store:

// vue component

@Component({})
export default class MyComponent extends Vue {

 @Inject() store: MyStore;

 mounted() {
  console.log(this.store.filtered); // always undefined :(
 }

}
// store

@VueClassStore
export class MyStore {

 get filtered(): string[] {
  return ['a', 'b'];
 }

}
davestewart commented 3 years ago

Hello.

That's strange. It should work.

What happens if you inspect the component in the Vue DevTools?

Also, what is your actual import in the store file?

Rigidoont commented 3 years ago

When I inspect the component I see the store with only the data property, no computed.

An example from a real component in my project: chrome_vRH6KoUwWy

In the store there is a filtered getter, but no computed property in inspector is present.

Import in the store file looks exactly like this: import VueClassStore from 'vue-class-store';

davestewart commented 3 years ago

What happens if you don't use class components?

Also, what happens if you set the store as a window property in your main file, and reference the computer property from there (just to debug) ?

Rigidoont commented 3 years ago

In plain vue, without class components the same thing happens.

I'm using nuxt so I don't really have a main file. I've created a simple plugin and put it on the top so it would run sooner than others, computed property is still empty.

davestewart commented 3 years ago

That's really strange, and I'm sorry it's happening; it must be frustrating.

Is there some way you could share a simple repo I could look at?

Rigidoont commented 3 years ago

Well the good news is that I resolved the problem. The bad news is that it took me so long... It seems I had v2.0.0 of the package installed and it refused to be updated for some reason. I completely reinstalled the package and everything works now. Should have started with that.

Thanks a lot for your help, I didn't at all expect such quick responses! Great package!

davestewart commented 3 years ago

Good news!

No problem, glad to help, and the package is still quite new so I'm keen to help people where they need it and fix problems that may arise!

I hope you enjoy using the package.

TTFN