【vue】vue +element 搭建项目,vuex中的store使用
一、概述:
每一个 Vuex 应用的核心就是 store(仓库)。“store”基本上就是一个容器,它包含着你的应用中大部分的状态 (state)。Vuex 和单纯的全局对象有以下两点不同:
-
Vuex 的状态存储是响应式的。当 Vue 组件从 store 中读取状态的时候,若 store 中的状态发生变化,那么相应的组件也会相应地得到高效更新。
-
你不能直接改变 store 中的状态。改变 store 中的状态的唯一途径就是显式地提交 (commit) mutation。这样使得我们可以方便地跟踪每一个状态的变化,从而让我们能够实现一些工具帮助我们更好地了解我们的应用。
- store中 state为属性(vuex状态)
- store中 getter为计算属性
- store中 mutation用于更改状态
- mutation必须是同步函数。
- 采用
store.commit
方法触发 - store中的action类似于mutation,
- Action 提交的是 mutation,而不是直接变更状态。
- Action 可以包含任意异步操作。
- Action 通过
store.dispatch
方法触发
二、应用:
1.依赖安装
1 | npm install vuex --save |
2.在src目录下新建文件夹 store,在该文件夹下创建store.js(此用法有报错,见下)
store.js
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment(state) {
state.count++
}
}
})
export default store
main.js
import Vue from 'vue' import App from './App' import router from './router' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' import store from './store' Vue.config.productionTip = false Vue.use(ElementUI); new Vue({ router, store, template: '<App/>', components: { App }, }).$mount('#app')
效果:(./store in ./src/main.js)
解决方案:将store.js更名为index.js
index.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | < template > < div class="app-container"> < span >home</ span > </ div > </ template > < script > export default { name:'home', created(){ this.$store.commit('increment'); console.log(this.$store.state.count); }, } </ script > |
效果:
3.store用法2
在store文件夹下新建 一个名为modules文件夹,在此文件夹下创建practice.js
practice.js
/**
* 用于学习store
*/
const practice = {
state: {
count: 0
},
mutations: {
increment(state) {
state.count++
}
},
}
export default practice
stroe文件下创建getters.js
const getters = {
practice_count: state => state.practice.count,
};
export default getters
store文件夹下创建index.js
import Vue from "vue";
import Vuex from "vuex";
import practice from './modules/practice';
import getters from './getters';
Vue.use(Vuex);
const store = new Vuex.Store({
modules: {
practice
},
getters
});
export default store
store.vue
<template> <div class="app-container"> <div class="title">store-getter用法</div> <div class="padding-xxs">count值:{{practice_count}}</div> <div class="padding-xxs">count值2:{{$store.getters.practice_count}}</div> </div> </template> <script> import {mapGetters} from 'vuex'; export default { name:'vueStoreDemo', data() { return { } }, computed: { ...mapGetters([ 'practice_count' ]) }, created(){ this.$store.commit('increment') }, methods: {}, } </script> <style> .title{ font-size: 14px; padding-left: 20px; color: #333; line-height: 34px; background-color: #F5F5F5; } </style>
效果:
....未完待续
参考资料:
作者:smile.轉角
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我QQ

分类:
vue
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通