经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
vuex的核心概念和基本使用详解
来源:jb51  时间:2021/12/15 8:46:18  对本文有异议

介绍

Vuex是实现组件全局状态(数据)管理的一种机制,可以方便的实现组件之间的数据共享

开始

安装

①直接下载方式

创建一个 vuex.js 文件 将https://unpkg.com/vuex这个网址里的内容放到该文件夹里。

②CND方式

  1. <script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.js"></script>

③NPM方式

  1. npm install vuex --save

④Yarn方式

  1. yarn add vuex

NPM方式安装的使用方式

1.在 scr 文件里创建一个 store / index.js 的文件夹,写入以下内容。

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. export default new Vuex.Store({
  5. state: {},
  6. mutations: {},
  7. actions: {},
  8. modules: {}
  9. })

2.在main.js 里引入,然后挂载到 Vue 实例里

  1. import Vue from 'vue'
  2. import store from './store'
  3. new Vue({
  4. render: h => h(App),
  5. store
  6. }).$mount('#app')

store概念及使用

概念:

就是组件之间共享数据的。

只有 mutations 才能修改 store 中的数据

使用:

先定义后使用

定义

  1. state: {
  2. num: 0
  3. }

使用

方式1(推荐)

  1. <div>{{ numAlias }}</div>
  2.  
  3. import { mapState } from 'vuex'
  4. export default {
  5. //计算函数
  6. computed: mapState({
  7. // 传字符串参数 'count' 等同于 `state => state.count`
  8. numAlias: 'num',//常用key是自己起的名随便 value接收的数据
  9. // 箭头函数可使代码更简练
  10. count: state => state.count,
  11. // 为了能够使用 `this` 获取局部状态,必须使用常规函数
  12. countPlusLocalState (state) {
  13. return state.count + this.localCount
  14. }
  15. //可以定义其余的计算函数
  16. }),
  17. //或者这样
  18. //计算函数
  19. computed: {
  20. mapState(['count'])
  21. }
  22. }

方式2

  1. <div>{{ $store.state.count }}</div>

mutations概念及使用

概念:

修改store里的数据,严格规定不能在其余的地方修改store的数据,mutations里不要执行异步操作。

mutation 必须同步执行,不能异步执行。

使用:

先定义方法后使用

定义

  1. mutations: {
  2. //increment自定义方法 store参数是store数据, parameter参数是接收到的数据,可不要
  3. increment (state, parameter) {
  4. // 变更状态
  5. state.num++
  6. }
  7. }

使用

方式1(推荐使用)

  1. import { mapState, mapMutations } from 'vuex'
  2. //方法
  3. methods: {
  4. ...mapMutations([
  5. // mutations自定义的方法名
  6. 'increment'
  7. ]),
  8. love() {
  9. // 直接this调用 this.increment('需要传过去的数据,可不要')
  10. this.increment('Bin')
  11. }
  12. }

方式2

  1. methods: {
  2. love() {
  3. // this.$store.commit('自定义的名称', '传过去的数据,可不传')
  4. this.$store.commit('increment', 'data')
  5. }
  6. }

action概念及使用

概念:

用于处理异步操作。

如果通过异步操作变更数据,必须通过action,而不能使用mutation,但是在action中还是要通过触发mutation的方式间接变更数据。

Action 类似于 mutation,不同在于:

  • Action 提交的是 mutation,而不是直接变更数据(状态)。
  • Action 可以包含任意异步操作。

定义

  1. mutations: {
  2. //increment自定义方法 store参数是store数据, parameter参数是接收到的数据,可不要
  3. increment (state, parameter) {
  4. // 变更状态
  5. state.num++
  6. }
  7. },
  8. actions: {
  9. //add 自定义方法 context是参数,可以把它当作vuex的实例
  10. add(context) {
  11. //可以通过context.commit('mutations中需要调用的方法')
  12. context.commit('increment')
  13. }
  14. }

使用

方式1(推荐)

  1. import { mapState, mapMutations, mapActions } from 'vuex'
  2. export default {
  3. methods: {
  4. ...mapActions([
  5. 'add', // 将 `this.add()` 映射为 `this.$store.dispatch('add')`
  6. // `mapActions` 也支持载荷:
  7. 'add' // 将 `this.add(amount)` 映射为 `this.$store.dispatch('add', amount)`
  8. ]),
  9. ...mapActions({
  10. add: 'add' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`
  11. }),
  12. love() {
  13. // 直接this调用 this.add('需要传过去的数据,可不要')
  14. this.add(data)
  15. }
  16. }
  17. }

方式2

  1. methods: {
  2. love() {
  3. // this.$store.dispatch('自定义的名称', '传过去的数据,可不传')
  4. this.$store.dispatch('add', data)
  5. }
  6. }

getters概念及使用

概念:

getter用于对store中的数据进行加工处理形成新的数据。getting可以对store中已有的数据加工处理之后形成新的数据,类似Vue的计算缩写。

定义

  1. state: {
  2. num: 0
  3. },
  4. getters: {
  5. doneTodos: state => {
  6. return state.num = 10
  7. }
  8. }

使用

方式1(推荐)

  1. <div>{{ doneTodos }}</div>
  2.  
  3. import { mapState, mapMutations, mapActions, mapGetters } from 'vuex'
  4. export default {
  5. //计算函数
  6. computed: {
  7. ...mapState(['count']),
  8. ...mapmapGetters(['doneTodos'])
  9. }
  10. }

方式2

  1. <div>{{ $store.getters.doneTodos }}</div>

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注w3xue的更多内容!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号