首先需要在 Vue 項目中繼承 typescript
vue add typescript
提示:如果配置完 ts 后調用 this.$store 有警告信息,請重啟 vscode,或者安裝 vue3 的插件后重啟 vscode 充實
一、修改 store.js 為 store.ts
二、配置 store.ts 中的代碼
Vuex 與 TypeScript 一起使用時,必須聲明自己的模塊擴充
//https://next.vuex.vuejs.org/guide/typescript-support.html#typing-store-property-in-vue-component
import { Store, createStore } from 'vuex'
declare module '@vue/runtime-core' {
// declare your own store states
interface State {
count: number,
list:string[],
msg:string
}
// provide typings for `this.$store`
interface ComponentCustomProperties {
$store: Store<State>
}
}
const store = createStore({
state() {
//數據
return{
}
},
mutations: {
},
getters: {
},
actions: {
}
})
export default store
持續更新中......