Vue + TypeScript 的項目里面繼承 Vuex


首先需要在 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

持續更新中......


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM