上手vuex

简介: 上手vuex

1.vuex是什么?


Vuex是一个为Vue.js开发的状态管理模式, 在我们之前学习Vue.js的时候,组件之间共享数据分为父传子(props),子传父($emit),兄弟组件(EventBus),但是关系复杂组件之间数据共享数据依旧很麻烦。有了vuex我们可以解决不同组件数据共享的很多复杂问题


1. state:共享数据状态,共享数据可以在组件中使用,要修改state状态必须通过mutations


2.mutations:执行同步操作,修改 state状态


3.action: 执行异步操作,异步执行后的数据提交给 mutations


2.vuex安装初始化


第一步:安装npm i vuex --save


第二步: 在main.js中import Vuex from 'vuex'


第三步: 在main.js调用vuex的install方法Vue.use(Vuex)


第四步: 生成store实例const store = new Vuex.Store({ })


第五步: 配置store选项new Vue({ el: '#app', store })


3.vuex使用(state)


定义 state


const store = new Vuex.Store({
  state: {
    // 全局共享数据
    count: 0
  }
})


获取数据


  • 原始形式 - 插值表达式


<div> state 的数据是: {{ $store.state.count }} <div>


  • 计算属性 - computed


<div> state 的数据是: {{ count }} <div>


computed: {
  count () {
    return this.$store.state.count
  }
}


  • 辅助函数 - mapState


<div> state 的数据是: {{ count }} <div>


import { mapState } from 'vuex'


computed: {
  ...mapState(['count'])
}


4.vuex的使用(mutations)


定义 mutations


const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
  }
})


书写 mutations修改state的方法


mutations: {
  // 第一个参数为当前 store中state属性
  // 第二个参数为载荷,可以通过调用时传递参数过来
  addCount (state, payload) {
    state.count += payload
  }
}


调用 mutations


  • 原始形式 - $store


<template>
  <button @click="addCount">+1</button>
</template>
<script>
export default {
  methods: {
    addCount() {
      // 通过commit提交muations
      // 10 表示传递到 payload的实参
      this.$store.commit('addCount', 10)
    }
  } 
}
</script>


  • 辅助函数 - mapMutations


<template>
  <button @click="addCount(10)">+10</button>
</template>
<script>
export default {
  import { mapMutations } from  'vuex'
  methods: {
    ...mapMutations(['addCount])
  } 
}
</script>


5.vuex的使用(actions)


定义 actions


actions: {
  // context 表示当前 store 的实例
  getAsyncCount (context) {
    setTimeout(function(){
      context.commit('addCount', 123)
    }, 1000)
  }
}


调用 actions


  • 原始调用 - $store


addAsyncCount () {
  this.$store.dispatch('getAsyncCount', 123)
}


  • 辅助函数 - mapActions


<button @click="getAsyncCount(100)">异步</button>


import { mapActions } from 'vuex'
methods: {
   ...mapActions(['getAsyncCount'])
}


6.vuex的使用(getters)


定义gettters


state: {
  list: [1,2,3,4,5,6,7,8,9]
}
getters: {
  filterList: state => state.list.filter(item => item > 5)
}


使用 getters


  • 原始方法 - $store


<div>{{ $store.getters.filterList }}</div>


  • 辅助函数 - mapGetters


<div>{{ filterList }}</div>


import { mapGetters } from 'vuex'
computed: {
  ...mapGetters(['filterList'])
}


7.vuex的使用(Module)


前面使用的这些都是全局性的共享, 如果使用这种方法, 当项目规模大时, vuex会越来越臃肿, 并且难以维护。vuex模块化可以解决这个问题。


应用:


定义两个模块 user 和 login

const store = new Vuex.Store({
  modules: {
    user: {
      state: {
        name: 'xiaoming'
      }
    },
    login: {
      state: {
        token: '123'
      }
    }
  }
})


使用子模块


  • 通过 $store.state.模块名.属性名


<div>用户名: {{ $store.state.user.name }}<div>
<div>token: {{ $store.state.login.token }}<div>


// 优化
getters: {
  name: state => state.user.name,
  token: state = > state.login.token
}


  • 通过 mapGetters使用


computed: {
  ...mapGetters(['name', 'token'])
}


命名空间 namespaced


默认情况下定义的模块依旧在全局共享, 这并不时我们想要的效果, 这是我们得用到namespaced给子模块加锁


给子模块加锁


login: {
  namespaced: true,
  state: {
    token: '123'
  },
  mutations: {
    updateToken(state) {
      state.token = 678 
    }
  }
}


给模块加锁后 调用 action/mutations时要带上属性名路径


  • 直接调用-带上模块的属性名路径


test () {
  this.$store.commit('login/updateToken') // 直接调用方法
}


  • 辅助函数-带上模块的属性名路径


methods: {
       ...mapMutations(['login/updateToken']),
       test () {
           this['user/updateToken']()
       }
   }
  <button @click="test">修改token</button>


  • createNamespacedHelpers


import { mapGetters, createNamespacedHelpers } from 'vuex'
const { mapMutations } = createNamespacedHelpers('login')
<button @click="updateToken">修改token2</button>
相关文章
|
7天前
|
JavaScript API iOS开发
DeepSeek Harness下载、安装、配置一篇搞定(2026整理,送常用插件)
DeepSeek Harness(DSH)是DeepSeek于2026年开源的MIT协议AI Agent框架,支持插件化组装模型、工具、沙箱与UI,兼容主流大模型。本文提供免Node.js的桌面端安装包(Win/macOS),含详细图文教程及热门插件推荐。
3333 30
|
7天前
|
Linux 开发工具 iOS开发
Codex保姆级教程(下载+安装+登录+使用,含所有版本,2026最新)
Codex是OpenAI推出的开源AI编程智能体,本地运行,可理解整个代码库,支持自然语言指令改代码、执行命令、跑测试、修Bug,兼容Win/macOS/Linux。(239字)
|
2天前
|
云安全 人工智能 安全
|
7天前
|
iOS开发 Docker MacOS
【2026实测】Docker Desktop下载、安装、汉化、配置教程(附官网安装包)
Docker Desktop 是 Docker 官方推出的桌面容器管理工具,集成引擎、CLI、图形界面与 Kubernetes,支持 Win/macOS/Linux。2026 最新版 4.88.1 强化 ARM 原生运行、镜像加速及资源可视化,附中文汉化与国内镜像配置指南。
1465 5
|
云安全 安全 网络安全
|
8天前
|
安全 算法 定位技术
《高性价比人生指南》完整原版:普通人用最少的资源,过好这一生的底层逻辑~How To Live Better
📖《高性价比人生指南》是一份基于实证、拒绝焦虑的生存手册,倡导以“人生时薪”为视角,重新计算时间、金钱与精力的成本与收益。它不承诺逆袭,只教人算清账:省下的钱若耗掉时间便亏;买体验比买物品更值;睡眠是最高杠杆的健康投资;关系贵在情绪价值而非表面热络;学习重在改变行为而非自我安慰。核心理念:用低成本、高回报的微小行动,换取长期的选择权——不是成为谁,而是掌控自己人生的主动权。
|
20天前
|
人工智能 API 开发工具
DeepSeek Harness 神插件:白嫖 13 家大厂模型
`dsh-our-free-model` 是 DeepSeek Harness 神级插件:零配置、零API Key、零账号,聚合13家大厂免费模型(含DeepSeek V4.1 Flash、Kimi K3等),支持本地网关、用量看板与自动降级,真正实现“白嫖即用”。
3598 53
|
12天前
|
人工智能 JSON Linux
【保姆级】ComfyUI部署教程(下载+安装+配置+工作流,2026最新)
ComfyUI 是一款开源节点式AI生成工具,支持图像、视频、音频及3D内容创作。通过拖拽连线组合模型加载、提示词、采样等节点,构建可复用、易修改的工作流,兼容SD1.5、SDXL、Flux、Qwen Image等主流模型。