admin管理员组文章数量:1410730
- Follow the official example to export your own
useStore
, and then use it in the ponent.
import { createStore, Store, useStore as baseUseStore } from 'vuex';
export const key: InjectionKey<Store<RootState>> = Symbol();
export function useStore() {
return baseUseStore(key);
}
use in the ponent
setup() {
const store = useStore();
const onClick = () => {
console.log(store)
store.dispatch('user/getUserInfo');
}
return {
onClick,
}
},
- After running,
store
is undefined. - It can be obtained normally when I use it in the
methods
attribute
methods: {
login() {
this.$store.dispatch('user/getToken')
}
}
- why? how to fix it
- Follow the official example to export your own
useStore
, and then use it in the ponent.
import { createStore, Store, useStore as baseUseStore } from 'vuex';
export const key: InjectionKey<Store<RootState>> = Symbol();
export function useStore() {
return baseUseStore(key);
}
use in the ponent
setup() {
const store = useStore();
const onClick = () => {
console.log(store)
store.dispatch('user/getUserInfo');
}
return {
onClick,
}
},
- After running,
store
is undefined. - It can be obtained normally when I use it in the
methods
attribute
methods: {
login() {
this.$store.dispatch('user/getToken')
}
}
- why? how to fix it
1 Answer
Reset to default 8In that simplifying useStore
usage tutorial, you still need to register the store
and key
in main.ts as they did. You will get undefined if you don't do this:
// main.ts
import { store, key } from './store'
const app = createApp({ ... })
// pass the injection key
app.use(store, key)
The reason is that baseUseStore(key)
has no meaning until that's done.
本文标签: javascriptUsing useStore API with vuex 4xStack Overflow
版权声明:本文标题:javascript - Using `useStore` API with vuex 4.x - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744230677a2596316.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论