Vuex
| vuex 是专为vue.js应用程序开发的管理模式,它采用集中式储存管理应用的去全部组件状态,并以响应的规则邦正状态可以以一种可预测的方式变化 |
| |
| |
| 主要管理数据 |
| |
| 使用vuex的时机,vuex相当于商店,谁都可以取买卖东西 |
| 当两个组件没有关系的情况下 |
| 1.如果项目中型或者大型,不推荐 使用 父子组件传值 |
| 2.中央事件总线可以推荐使用 中型项目 设置bus 使用emit 和 on 实现 |
| 项目足够大的时候 中央事件总线不可以,太臃肿 |
| 3.使用vuex 类似一个单例的对象 vuex管理状态(想共享的状态(数据)) 大型 |
| 共享的数据放到vuex |
| 加入购物车 - vuex 中的Actions(异步的)中的方法 -提交-mutations(同步的)-修改- state:{ 共享的数据 状态} - 渲染- 需要使用这个数据的组件中 |
| 登录注册,购物车 ,权限控制,组件之间不好通信的时候就会使用vuex |
| 中大型项目中,组件之间不好通信 就会使用vuex |
| 修改数据的唯一方法 就是actions方法中提交mutations方法 |
| |
| |
| vuexAPI:https://vuex.vuejs.org/zh/ |
目录
| store/index.js |
| import Vue from 'vue' |
| import Vuex from 'vuex' |
| |
| Vue.use(Vuex) |
| |
| export default new Vuex.Store({ |
| state: { |
| }, |
| getters: { |
| }, |
| mutations: { |
| }, |
| actions: { |
| }, |
| modules: { |
| } |
| }) |
| |
| 在main.js 对vuex进行挂载 |
| import Vue from 'vue' |
| import App from './App.vue' |
| import store from './store' |
| |
| Vue.config.productionTip = false |
| |
| new Vue({ |
| store, |
| render: h => h(App) |
| }).$mount('#app') |
使用步骤
| 1.在state对像 中设置公共的状态参数 |
| 2.在 mutations中设置一个函数负责对这个参数 进行变化和修改(设置的函数中需要接受参数state对象) |
| 3. 在actions 中设置一个函数,使用commit进行执行在mutations中函数 |
| 4.在对应的组件中设置的按钮中,绑定一个函数,函数内容需要执行actions中的设置的函数 |
vuex的基本使用
在组件中使用vuex中的state内数据
| export default new Vuex.Store({ |
| state: { |
| count:10, |
| }, |
| .... |
| } |
| |
| |
| this.$store.state.count |
在组件中使用调用vuex中getters内方法
| export default new Vuex.Store({ |
| state: { |
| count:10, |
| }, |
| getters: { |
| getCount(state,getters){ |
| |
| |
| console.log(state) |
| return state.count+10 |
| } |
| }, |
| ..... |
| } |
| |
| |
| this.$store.getters.getCount |
| or |
| computed: { |
| count() { |
| return this.$store.getters.getCount |
| } |
| } |
在组件中的使用mutation同步方法
| export default new Vuex.Store({ |
| state: { |
| count:10, |
| }, |
| getters: { |
| }, |
| mutations: { |
| getInfo(state,val){ |
| |
| |
| state.count += val |
| } |
| }, |
| ..... |
| } |
| |
| 在组件中进行调用: |
| methods:{ |
| show(){ |
| commit('mutations中的方法名','传入的变量') |
| this.$store.commit('getInfo',10) |
| } |
| }, |
| |
| 只能进行同步操作 |
| 每个mutation执行完成后都会对应到一个新的状态变更,这样devtools就可以打个快照存下来,然后就可以实现 time-travel 了。 |
| 如果mutation支持异步操作,就没有办法知道状态是何时更新的,无法很好的进行状态的追踪,给调试带来困难 |
在组件中的使用actions异步方法
| export default new Vuex.Store({ |
| state: { |
| count:10, |
| }, |
| getters: { |
| }, |
| mutations: { |
| getInfo(state,val){ |
| |
| |
| state.count += val |
| } |
| }, |
| actions: { |
| getInfo({commit},val){ |
| |
| commit('getInfo',val) |
| } |
| }, |
| modules: { |
| } |
| }) |
| |
| |
| 组件调用: |
| 通过$store.dispatch调用actions的方法 |
| this.$store.dispatch('getInfo',10) |
| |
| |
| |
| |
| |
| |
| |
actions与mutations为什么要使用或区别
| mutations: 声明的方法是同步的,就是页面的更新,不需要从后端获取请求 |
| actions:声明的方法时异步的(一旦异步就需要使用actions的方法)需要后端发起请求获取或者怎么样 |
| 两个之间的区别在哪里 |
| 修改状态(state对象的参数)的唯一方法就是提交commit |
| |
| |
| 触发dispatch,就是触发actions中的函数方法 |
| this.$store.dispatch('func') |
| 同步直接使用mutations中的方法就行 |
| this.$store.commit('func') |
| |
| |
| |
| 异步:实时更新,只要你操作就会先对你的值进行更新,然后在存储到数据库中 |
| 同步:当你点击对你的值进行更新时,需要先存入数据库,在进行显示 |
| 案例: |
| |
| export default new Vuex.Store({ |
| state: { |
| count: 10, |
| }, |
| getters: {}, |
| mutations: { |
| getInfo(state) { |
| setTimeout(() => { |
| state.count++ |
| }, 1000) |
| } |
| }, |
| actions: { |
| getInfo({commit}) { |
| setTimeout(() => { |
| commit('getInfo') |
| }, 1000) |
| } |
| }, |
| modules: {} |
| }) |
| |
| 组件内 |
| <template> |
| <div id="app"> |
| <div>{{this.$store.state.count}}</div> |
| <button @click="show1">异步+1操作</button> |
| <button @click="show2">同步+1操作</button> |
| </div> |
| </template> |
| |
| <script> |
| |
| |
| export default { |
| name: 'App', |
| components: {}, |
| data(){ |
| return{ |
| } |
| }, |
| methods:{ |
| show1(){ |
| this.$store.dispatch('getInfo') |
| }, |
| show2(){ |
| this.$store.commit('getInfo') |
| } |
| }, |
| |
| } |
| </script> |
| |
| <style lang="scss"> |
| |
| </style> |
| |
| 需要使用谷歌的vue组件进行查看 |
| 使用同步方式时: |
| 虽然页面显示的时增加的正确数值,但是在vue组件中监控的数值永远比页面中 少1 |
| |
| 使用异步的方式: |
| 页面显示的数值与监控的数值是相同的。 |
辅助函数的使用
| import {mapState,mapGetters,mapMutations,mapActions} from 'vuex' |
| |
| mapState 辅助获取状态中的参数 |
| mapMutations,mapActions 辅助获取同步异步的方法 |
| mapGetters 获取计算属性中的方法 |
| <template> |
| <div id="app"> |
| |
| <p>{{ count }}</p> |
| <p>{{ getCount }}</p> |
| <button @click="getInfo">点击</button> |
| </div> |
| </template> |
| |
| <script> |
| import {mapActions, mapState, mapGetters, mapMutations} from 'vuex' |
| |
| export default { |
| name: 'App', |
| components: {}, |
| data() { |
| return {} |
| }, |
| methods: { |
| ...mapActions(['getInfo']), |
| ...mapMutations(['getInfo']) |
| }, |
| computed: { |
| ...mapState(['count']), |
| ...mapGetters(['getCount']) |
| } |
| } |
| </script> |
vuex中modules嵌套使用
父
| import Vue from 'vue' |
| import Vuex from 'vuex' |
| import a from '@/store/xx/a' |
| import b from '@/store/xx/b' |
| |
| Vue.use(Vuex) |
| |
| export default new Vuex.Store({ |
| state: { |
| terr:1000 |
| }, |
| getters: {}, |
| mutations: {}, |
| actions: {}, |
| modules: { |
| |
| a, b |
| } |
| }) |
子1
| export default { |
| namespaced: true, |
| state: { |
| a: '我是a节点', |
| }, |
| getters: { |
| getCount(state, getters, rootState) { |
| |
| |
| |
| return rootState.b.b |
| } |
| }, |
| mutations: { |
| show(state) { |
| |
| console.log('sb') |
| } |
| }, |
| actions: { |
| show({commit, state, rootState}) { |
| |
| |
| |
| |
| |
| |
| commit('b/show',{root:true}) |
| } |
| }, |
| } |
子2
| export default { |
| namespaced: true, // 命名空间后,就会根据注册的名字调整当前的路径,这样的话就会进行分开 |
| state: { |
| b: '我是b节点' |
| }, |
| getters: {}, |
| mutations: {}, |
| actions: { |
| shows() { |
| console.log('b节点') |
| } |
| }, |
| } |
在组件中使用
| <template> |
| <div id="app"> |
| |
| <p>{{ numShow }}</p> |
| <p>{{ a }}</p> |
| <p>{{getCount}}</p> |
| |
| <button @click="show">点击</button> |
| </div> |
| </template> |
| |
| <script> |
| import {mapActions, mapState, mapGetters, mapMutations} from 'vuex' |
| |
| export default { |
| name: 'App', |
| components: {}, |
| data() { |
| return {} |
| }, |
| methods: { |
| shows() { |
| |
| this.$store.dispatch('a/show') |
| }, |
| |
| ...mapActions('a',['show']) |
| }, |
| computed: { |
| |
| ...mapState('a', ['a']), |
| ...mapState('a', ['b']), |
| numShow() { |
| return this.$store.state.a.a |
| }, |
| ...mapGetters('a',['getCount']) |
| } |
| } |
| </script> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现