typescript - useStore() method always leading to undefined value within components
问题描述
I am using Vue3/Vuex 4/TypeScript.
I am trying to access my typed store from within my component (App.vue
).
Whenever I set const store = useStore()
, store
will always return as undefined
As far as I can tell, I've followed the official Vuex 4 TypeScript Support Documentation verbatim.
app.ts
import { createApp, Component } from "vue"
import { createWebHistory, createRouter } from "vue-router"
import { store, key } from "./store/store"
import App from "./pages/App.vue"
import Home from "./pages/Home.vue"
import Lookup from "./pages/Lookup.vue"
const About = { template: "<div>About</div>" }
const routes = [
{ path: "/", name: "home", component: Home as Component },
{ path: "/about", name: "about", component: About },
{ path: "/lookup", name: "lookup", component: Lookup as Component },
]
const router = createRouter({
history: createWebHistory(),
routes,
})
createApp(App).use(router, store, key).mount("#app")
export default router
store.ts
import { InjectionKey } from 'vue'
import { createStore, useStore as baseUseStore, Store } from 'vuex'
export interface State {
count: number
}
export const key: InjectionKey<Store<State>> = Symbol()
export const store = createStore<State>({
state: {
count: 0
}
})
// define your own `useStore` composition function
export function useStore () {
return baseUseStore(key)
}
App.vue
<script lang="ts">
import { useStore } from '../store/store'
export default {
setup(): { [key: string]: unknown } {
const foo = "bar"
const store = useStore()
console.log(store)
console.log(store.state.count)
return { foo }
},
}
</script>
store is always returning as undefined, here's my console messages from FireFox
[Vue warn]: injection "Symbol()" not found.
at <App> app.js:5871:17
undefined app.js:16779:13
[Vue warn]: Unhandled error during execution of setup function
at <App> app.js:5871:17
Uncaught TypeError: store is undefined
setup http://boilerplate.local/js/app.js:16780
callWithErrorHandling http://boilerplate.local/js/app.js:5987
setupStatefulComponent http://boilerplate.local/js/app.js:12359
setupComponent http://boilerplate.local/js/app.js:12320
mountComponent http://boilerplate.local/js/app.js:10027
processComponent http://boilerplate.local/js/app.js:10003
patch http://boilerplate.local/js/app.js:9621
render http://boilerplate.local/js/app.js:10704
mount http://boilerplate.local/js/app.js:8907
mount http://boilerplate.local/js/app.js:14290
<anonymous> http://boilerplate.local/js/app.js:23012
<anonymous> http://boilerplate.local/js/app.js:23014
<anonymous> http://boilerplate.local/js/app.js:23016
I've tried to debug this all day but really can't narrow in on the problem. Any help/guidance is greatly appreciated.
解决方案
您将 Vuex 实例安装到您的应用程序的语法有点偏离。在每次调用 时,您只能将一个 Vue 插件安装到应用程序中use
,因此这样的组合router, store, key
是行不通的。因此,根本没有安装 Vuex。
要修复,请更改此行:
createApp(App).use(router, store, key).mount("#app")
对此:
createApp(App).use(router).use(store, key).mount("#app")
(key
只是传递给的一个选项store
,因此它确实属于第二次调用。)
推荐阅读
- wordpress - 按功能更改 xml 自定义提要后出错
- java - 在java中将不同的对象添加到列表中
- java - Java / Jackson - 'Unrecognized token' 传递 JSON 对象参数
- javascript - 如何使用 NodeJS 应用程序文件动态地将 CSS 和 JS 排入页面
- scala - 当 JOIN 列不同时,使用 Spark Scala 动态连接数据帧
- javascript - 需要 Javascript 新运算符帮助 - 在 javascript 中使用新运算符的动态对象
- mysql - 创建表时 MySQL 十进制语法错误
- android - 如何实现相同的转换?
- java - 每个模块(在多模块 Maven 项目中)是否应该有自己的专用异常?
- sql - 使用 SQL Server 将多个 Like 行合并为一行