记录使用vscode构建Vue3商店后台管理系统,这是第七篇,主要记录系统登录页面的创建过程,包含完整vue登录页面代码;Vuex的相关知识以及具体的使用,对state中值得获取,修改,异步修改,分模块用法进行详细记录,包含完整代码示例
正文内容:
{path: "/login",name: "loginView",component: () => import("../views/login/LoginView.vue"),}
商品后台管理系统
登录
Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式 + 库。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。
import {createStore} from "vuex";export default createStore({// 全局的状态初始值state: {count: 1,},// 计算state,获取对应值getters: {},// 更新状态的方法,更新state的唯一方法,commit mutationsmutations: {},// 可以异步操作,可以返回promise,更改数据还是传递到mutations进行更改actions: {},// 数据比较多的时候,分模块modules: {},
});
import { useStore } from "vuex"export default {name: "login",setup() {const store = useStore()const storeNum = store.state.count;const data = reactive({num: storeNum,})return {...toRefs(data),}}
}
import {createStore} from "vuex";export default createStore({// 全局的状态初始值state: {count: 1,},// 计算state,获取对应值getters: {},// 更新状态的方法,更新state的唯一方法,commit mutationsmutations: {setCount(state, num) {state.count = num;},},// 可以异步操作,可以返回promise,更改数据还是传递到mutations进行更改actions: {},// 数据比较多的时候,分模块modules: {},
});
import { reactive, toRefs } from "vue"
import { useStore } from "vuex"export default {name: "login",setup() {const store = useStore()const storeNum = store.state.count;const data = reactive({num: storeNum,})const submitForm = function (ruleFormRef) {console.log("login");//通过commit方法更改count值,第一个参数是mutations里面具体的方法名称,第二个参数是count的修改值store.commit("setCount", 100);console.log(store.state.count);}return {...toRefs(data),submitForm}}
}
import {createStore} from "vuex";export default createStore({// 全局的状态初始值state: {count: 1,},// 计算state,获取对应值getters: {},// 更新状态的方法,更新state的唯一方法,commit mutationsmutations: {setCount(state, num) {state.count = num;},},// 可以异步操作,可以返回promise,更改数据还是传递到mutations进行更改actions: {setCountPromise(context, num) {return new Promise((resolve, reject) => {if (num > 100) {reject("数值不能大于100");} else {context.commit("setCount", num);resolve();}});},},// 数据比较多的时候,分模块modules: {},
});
const submitForm = function (ruleFormRef) {//通过dispatch方法更改count值,第一个参数是actions里面具体的方法名称,第二个参数是count的修改值store.dispatch("setCountPromise", 101).then(resp => { console.log(store.state.count); }).catch(err => { console.log(err) });
}
import {createStore} from "vuex";export default createStore({// 全局的状态初始值state: {count: 1,},// 计算state,获取对应值getters: {getCount(state) {return state.count <= 1;},},// 更新状态的方法,更新state的唯一方法,commit mutationsmutations: {setCount(state, num) {state.count = num;},},// 可以异步操作,可以返回promise,更改数据还是传递到mutations进行更改actions: {setCountPromise(context, num) {return new Promise((resolve, reject) => {if (num > 100) {reject("数值不能大于100");} else {context.commit("setCount", num);resolve();}});},},// 数据比较多的时候,分模块modules: {},
});
const submitForm = function (ruleFormRef) {store.commit("setCount", 100);console.log(store.getters.getCount);
}
store.state.count
也可以获取count值,但是为什么还需要store.getters.getCount
这种方式获取count值;首先笔者发现这是一种解耦的操作,通俗的将就是模块之间的耦合度降低,方便代码后期维护管理;通过store.getters.getCount
这种方式,对count的操作独立出来,代码后期需要更改时是十分方便的export default {//开启namespace:true,该模块就成为命名空间模块namespaced: true,// 全局的状态初始值state: {count: 1,},// 计算state,获取对应值getters: {getCount(state) {return state.count <= 1;},},// 更新状态的方法,更新state的唯一方法,commit mutationsmutations: {setCount(state, num) {state.count = num;},},// 可以异步操作,可以返回promise,更改数据还是传递到mutations进行更改actions: {setCountPromise(context, num) {return new Promise((resolve, reject) => {if (num > 100) {reject("数值不能大于100");} else {context.commit("setCount", num);resolve();}});},},
};
import {createStore} from "vuex";
import number from "./state/Number.state.js";
export default createStore({// 数据比较多的时候,分模块modules: {number},
});