gpt4 book ai didi

typescript - Vue.js 3和typescript:类型'ComponentPublicInstance上不存在 '$store'属性

转载 作者:行者123 更新时间:2023-12-03 06:43:43 26 4
gpt4 key购买 nike

找不到任何东西可以解决这个看似明显的问题。
刚刚从Type 2的Vue 2升级到Vue 3和Vuex。
尽管遵循Vue 3的说明,但似乎无法访问this。$ store。

ERROR in src/components/FlashMessages.vue:28:25TS2339: Property '$store' does not exist on type 'ComponentPublicInstance<{}, {}, {}, { getAllFlashMessages(): Word; }, {}, EmitsOptions, {}, {}, false, ComponentOptionsBase<{}, {}, {}, { getAllFlashMessages(): Word; }, {}, ComponentOptionsMixin, ComponentOptionsMixin, EmitsOptions, string, {}>>'.

    26 |     computed: {
27 | getAllFlashMessages (): FlashType {
> 28 | return this.$store.getters.getFlashMessages;
| ^^^^^^
29 | },
30 | },
31 |
主要
import { createApp } from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import store from './store'
import './assets/styles/index.css'

const app = createApp(App)

app.use(store)
app.use(router)
app.mount('#app')
商店
import { createStore } from 'vuex'
import FlashType from '@/init'

export default createStore({
state: {
flashMessages: [] as FlashType[],
},

getters: {
getFlashMessages (state) {
return state.flashMessages
}
},
FlashMessages.vue
<script lang="ts">
import { defineComponent } from "vue";
import FlashType from "@/init";

export default defineComponent({
name: "FlashMessages",

data () {
return {};
},

computed: {
getAllFlashMessages (): FlashType {
return this.$store.getters.getFlashMessages;
},
},
初始化
export type FlashType = {
kind: 'success' | 'error';
message: string;
time: number;
}
任何智慧表示赞赏:)
文件结构
├── .editorconfig
├── client
│   ├── babel.config.js
│   ├── CONTRACTS.md
│   ├── cypress.json
│   ├── jest.config.js
│   ├── package.json
│   ├── package-lock.json
│   ├── postcss.config.js
│   ├── public
│   │   ├── favicon.ico
│   │   ├── index.html
│   │   └── robots.txt
│   ├── README.md
│   ├── src
│   │   ├── App.vue
│   │   ├── assets
│   │   │   ├── logo.png
│   │   │   └── styles
│   │   │   └── index.css
│   │   ├── components
│   │   │   ├── admin
│   │   │   │   ├── AdminAdd.vue
│   │   │   │   ├── AdminList.vue
│   │   │   │   ├── AdminWord.vue
│   │   │   │   ├── EmotionAdd.vue
│   │   │   │   ├── EmotionsList.vue
│   │   │   │   └── Emotion.vue
│   │   │   └── FlashMessages.vue
│   │   ├── init.ts
│   │   ├── main.ts
│   │   ├── registerServiceWorker.ts
│   │   ├── router
│   │   │   └── index.ts
│   │   ├── shims-vue.d.ts
│   │   ├── store
│   │   │   └── index.ts
│   │   └── views
│   │   ├── About.vue
│   │   ├── Admin.vue
│   │   ├── Emotions.vue
│   │   └── Home.vue
│   ├── tsconfig.json
│   └── vue.config.js

└── server
├── api
├── api.bundle.js
├── index.ts
├── logger
│   └── logger.ts
├── models
├── nodemon.json
├── package.json
├── package-lock.json
├── router
│   ├── db.ts
│   └── emotions.ts
├── tsconfig.json
└── webpack.config.js
这是我第一次正确使用eslint,所以我不确定是否设置正确。.我最终在/ client和/ server目录中使用了另一个tsconfig。
客户端/ tsconfig.json
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": "./",
"types": [
"webpack-env",
"jest"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules"
]
}

最佳答案

shims-vue.d.ts文件旁边,创建另一个名为shims-vuex.d.ts的文件,其内容如下:

import { Store } from '@/store';// path to store file

declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
$store: Store;
}
}

关于typescript - Vue.js 3和typescript:类型'ComponentPublicInstance上不存在 '$store'属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64412243/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com