vue3安装vuex

npm install vuex@next --save

src文件下新建store -> index.ts文件,store -> appStore下新建state.tsgetters.tsactions.tsmutations.tsindex.ts文件

-store -> appStore -> index.ts 引入各个模块

import state from './state'
import actions from './actions'
import mutations from './mutations'
import getters from './getters'
const appStore = {
    state,
    mutations,
    actions,
    getters,
}
export default appStore;

-store -> appStore -> state.ts

export default {
    countVuex: 0,
}

-store -> appStore -> getters.ts

export default{
    countVuex: (state: any) => state.countVuex
}

-store -> appStore -> actions.ts

export default {
    ADD_ACOUNTVUEX(store: any, countVuex: Number) {
        store.commit('ADD_ACOUNTVUEX', countVuex)
    }
}

-store -> appStore -> mutations.ts

export default {
    ADD_ACOUNTVUEX(state: any, countVuex: Number) {
        state.countVuex = countVuex
    }
}
  • store -> index.ts文件 创建store
import { createStore } from 'vuex';
import appStore from "./appStore"

export default createStore({
  modules: {
      appStore
  }
})

组件中使用:js中用const store = useStore();引入;template中用$store.state.xxx引入



打包时候Cannot find name '$store'.
package.json改build打包命令"build": "vite build"npm run build

你可能感兴趣的:(vue3安装vuex)