VUEX各个模块的封装以及Router封装

本文详细介绍了Vue.js中状态管理库Vuex的使用方法,包括state、mutation、getter和action的作用,以及如何通过它们实现数据的共享、过滤、异步操作和持久化。同时,文章还提供了具体的代码示例,展示了如何在实际项目中配置和使用Vuex。

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

一、各个模块的作用:

  • state 用来数据共享数据存储
  • mutation 用来注册改变数据状态(同步)
  • getters 用来对共享数据进行过滤并计数操作
  • action 解决异步改变共享数据(异步)

二、 创建文件:

actions.js

getters.js

mutations.js

mutationsFun.js

state.js


三、编辑文件

//在store的文件下index.js引入
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from "vuex-persistedstate";//持久化
import * as actions from './actions'
import * as getters from './getters'
import state from './state'
import mutations from './mutations'

Vue.use(Vuex)

export default new Vuex.Store({
	actions,//actions:actions,
	getters,//getters:getters,
	state,//state:state,
	mutations,//mutations:mutations,
  	plugins: [createPersistedState()]// 数据持久化
})j

state.js
const state = {
	list:['1','2','3'],
    isShow:false,
    bool:true,
}

export default state

mutations.js

import * as Fun from './mutationsFun'	//*as默认导入的是一个对象
const mutations = {
	[Fun.set_List](state, list) {
		state.list = list//修改数据
    },
    [Fun.set_isShow](state, isShow) {
		state.isShow = isShow//修改数据
    },
    [Fun.set_Bool](state, bool) {
		state.bool = bool//修改数据
    }  

}

export default mutations
mutationsFun.js
export const set_List = 'set_List'
export const set_isShow = 'set_isShow'
export const set_Bool = 'set_Bool'
getters.js
export const list = state => state.list
export const isShow = state => state.isShow
export const bool = state => state.bool
actions.js
import * as Fun from './mutationsFun'
export const set_List= function (commit,list) {
	commit(Fun.set_List, list)
	commit(Fun.set_isShow, isShow)
	commit(Fun.set_Bool, bool)
}

路由封装

router文件夹下一个index.js 一个list.js 按需创建
//index.js
import Vue from "vue";
import VueRouter from "vue-router";
import routersList from "./list";
import HelloWorld from "@/components/HelloWorld";
Vue.use(VueRouter);
// 导入路由文件
const routes = [
  {
    path: "/",
    name: "HelloWorld",
    component: HelloWorld,
    redirect: "/index"
  },
  ...routersList//es6...托找运算符
];

const router = new VueRouter({
  routes
});

export default router;
//list.js
import msmk from "../views/msmk/msmk"; //底部tabbar
import index from "../views/msmk/index"; //首页
import course from "../views/msmk/course"; //课程
import record from "../views/msmk/record"; //约课记录
import practise from "../views/msmk/practise"; //联系

const routesList = [
  // 注册tabbar及子路由
  {
    path: "/msmk",
    name: "msmk",
    component: msmk,
    children: [
      {
        path: "/index",
        name: "index",
        component: index,
        meta: {
          title: "每时每课"
        }
      },
      {
        path: "/course",
        name: "course",
        component: course,
        meta: {
          title: "特约课"
        }
      },
      {
        path: "/record",
        name: "record",
        component: record,
        meta: {
          title: "约课记录"
        }
      },
      {
        path: "/practise",
        name: "practise",
        component: practise,
        meta: {
          title: "练习"
        }
      },
      {
        path: "/login",
        name: "login",
        component: login,
        meta: {
          title: "个人中心"
        }
      }
    ]
  },
  // 注册tabbar及子路由结束
];

export default routesList;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值