- Follow the official example to export your own
useStore, and then use it in the component.
import { createStore, Store, useStore as baseUseStore } from 'vuex';
export const key: InjectionKey<Store<RootState>> = Symbol();
export function useStore() {
return baseUseStore(key);
}
use in the component
setup() {
const store = useStore();
const onClick = () => {
console.log(store)
store.dispatch('user/getUserInfo');
}
return {
onClick,
}
},
- After running,
storeis undefined. - It can be obtained normally when I use it in the
methodsattribute
methods: {
login() {
this.$store.dispatch('user/getToken')
}
}
- why? how to fix it