vue3.0+vite+typescript项目架构搭建之VUEX配置

本文介绍了如何在Vue3.0项目中结合Vite和TypeScript搭建Vuex,使用vuex-module-decorators进行模块化管理。详细讲解了state、getter和setter、Mutation、Action以及MutationAction的配置与使用,并提供了具体案例和在login.vue页面的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vue项目自然少不了VUEX了,这里使用vuex-module-decorators进行模块化分割.

npm install vuex-module-decorators 或者
yarn add vuex-module-decorators

安装成功后就可以使用了,先看一个完整案例

1.创建主配置文件index.ts

/**
 * @description vuex 配置
 * @author : lsx
 * @version : v1.0
 * @date : 2020/10/22 14:33
 */
import {
   createStore} from "vuex";
import {
   config} from "vuex-module-decorators";
import {
   isDevMode} from "/@/utils/common";

config.rawError = true;

interface IState {
   
    loading: boolean;
}

export default createStore({
   
   state(): IState {
   
       return {
   
           loading: false,
       };
   },
   strict: isDevMode(),
   modules: {
   }
});

2.创建模块注册文件registerModule.ts

import store from "/@/store";

export function registerModule(name: string) {
   
    if (!name) {
   
        return;
    }
    if ((store.state as any)[name]) {
   
        store.unregisterModule(name);
    }
}

  1. 模块vuex文件详解, 以sign.ts为例
import {
    VuexModule, Module, getModule, Mutation, Action } from "vuex-module-decorators";
import store from "/@/store/index";
import {
    registerModule } from "/@/store/vuexRegisterModule";

import {
    ILoginData } from "/@/states/sign/login.interface";
import {
    LOGIN } from "/@/api/sign"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值