Possible to use parameters with useGetters?
tanc opened this issue · 5 comments
I have a find
getter on a bikes
namespaced store which takes a parameter. How would I use useGetters
with parameters?
For example, I have tried: const { find: findBikesInStore } = useGetters(['find'])
But that returns an empty object as it needs parameters to know what to query. I'm using the createNamespacedHelpers
function and can confirm it works with actions and state.
This is what I'm currently using locally which works:
$store.getters['bikes/find']({ query }).data
Is it possible to translate to a useGetters
syntax?
interesting.
can you supply a demo state with a demo example?
I'll add a unit test to check this use-case, and fix it accordingly.
thanks.
@tanc can you please provide a demo example of a store similar to yours?
It would be very helpful in order to fix your problem..
Thanks.
Yes sorry, I meant to reply before. I’ll try to get something together as a test case
@davidmeirlevy Here is a test case for what I think @tanc is trying to do. This is with the new typing PR, but should work without it as well. I did have to put a .value
after the getter variable as it's returned as a Ref<T>
.
it('should render component using a typed state getter with params', () => {
interface Getters {
valGetter: (state: any) => (_: string) => string;
};
const value = 'getter-demo' + Math.random();
const store = new Vuex.Store({
state: {
val: value
},
getters: {
valGetter: (state) => (prefix: string) => `${prefix}${state.val}`
}
});
const wrapper = shallowMount({
template: `<div>{{val}}</div>`,
setup() {
const {valGetter} = useGetters<Getters>(store, ['valGetter']);
const val = computed(() => valGetter.value('Foobar'))
return {
val
}
}
},
{localVue}
);
expect(wrapper.text()).toBe(store.getters['valGetter']('Foobar'));
});