五分钟带你玩转vue(八)vuex

举报
小鲍侃java 发表于 2021/09/10 22:49:49 2021/09/10
【摘要】 Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.详细介绍可以参照官网文档https://vuex.vuejs.org/zh/ 下面简单介绍vuex 安装和引入 project>npm install vuex --sa...

Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.详细介绍可以参照官网文档https://vuex.vuejs.org/zh/

下面简单介绍vuex

安装和引入

project>npm install vuex --save
 

然后就可以在main.js中使用


  
  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import router from './router'
  6. //vuex使用
  7. import Vuex from 'vuex'
  8. Vue.use(Vuex)
  9. const store = new Vuex.Store({
  10. state: {
  11. //全局变量
  12. count: 31231
  13. }
  14. })
  15. Vue.config.productionTip = false
  16. /* eslint-disable no-new */
  17. new Vue({
  18. el: '#app',
  19. router,
  20. //vuex必须加入
  21. store,
  22. components: { App },
  23. template: '<App/>'
  24. })

vuex的使用


  
  1. <template>
  2. <div>
  3. 老大有{{showData}}
  4. <HelloWorld2/>
  5. </div>
  6. </template>
  7. <script>
  8. import HelloWorld2 from './HelloWorld2'
  9. import son from './son'
  10. export default {
  11. name: 'HelloWorld',
  12. data () {
  13. return {
  14. message2:"",
  15. cou
  16. }
  17. },
  18. components:{
  19. HelloWorld2,
  20. son
  21. },computed: {
  22. showData(){
  23. return this.$store.state.count;
  24. }
  25. }
  26. }
  27. </script>

  
  1. <template>
  2. <div>
  3. 老二有{{$store.state.count}}
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'HelloWorld2',
  9. data() {
  10. return {
  11. }
  12. }
  13. }
  14. </script>

流程介绍

如图当没有使用vuex时流程为: view->actions->state->view

vuex

使用了vuex后流程为vuecomponent->(dispatch)actions->(commit)mutations->(mutate)state->(render)->vuecomponent

mutation

状态更改 更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。Vuex 中的 mutation 非常类似于事件:每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)。这个回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数。


  
  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import router from './router'
  6. //vuex使用
  7. import Vuex from 'vuex'
  8. Vue.use(Vuex)
  9. const store = new Vuex.Store({
  10. state: {
  11. //全局变量
  12. count: 31231
  13. },
  14. //更改状态方法
  15. mutations: {
  16. //state为上面的state
  17. addData(state) {
  18. // 变更状态
  19. state.count++
  20. }
  21. }
  22. })
  23. Vue.config.productionTip = false
  24. /* eslint-disable no-new */
  25. new Vue({
  26. el: '#app',
  27. router,
  28. //vuex必须加入
  29. store,
  30. components: { App },
  31. template: '<App/>'
  32. })

然后执行更改


  
  1. <template>
  2. <div>
  3. 老大有{{showData}}
  4. <HelloWorld2/>
  5. <button type = "button" v-on:click = "changeData"> 修改按钮 </button>
  6. </div>
  7. </template>
  8. <script>
  9. import HelloWorld2 from './HelloWorld2'
  10. import son from './son'
  11. export default {
  12. name: 'HelloWorld',
  13. data () {
  14. return {
  15. message2:"",
  16. }
  17. },
  18. components:{
  19. HelloWorld2,
  20. son
  21. },computed: {
  22. showData(){
  23. return this.$store.state.count;
  24. }
  25. },
  26. methods: {
  27. //执行更改
  28. changeData(event){
  29. this.$store.commit("addData");
  30. }
  31. }
  32. }
  33. </script>

getters过滤

可以限制mutation 比如小于0就不能减少了


  
  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import router from './router'
  6. //vuex使用
  7. import Vuex from 'vuex'
  8. Vue.use(Vuex)
  9. const store = new Vuex.Store({
  10. state: {
  11. //全局变量
  12. count: 0
  13. },
  14. //更改状态方法
  15. mutations: {
  16. //state为上面的state
  17. addData(state) {
  18. // 变更状态
  19. state.count++
  20. }
  21. },
  22. //过滤
  23. getters: {
  24. getState(state) {
  25. if (state.count >= 5) {
  26. return 5
  27. } else {
  28. return state.count
  29. }
  30. }
  31. }
  32. })
  33. Vue.config.productionTip = false
  34. /* eslint-disable no-new */
  35. new Vue({
  36. el: '#app',
  37. router,
  38. //vuex必须加入
  39. store,
  40. components: { App },
  41. template: '<App/>'
  42. })

调用时


  
  1. <template>
  2. <div>
  3. 老二有{{$store.getters.getState}}
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'HelloWorld2',
  9. data() {
  10. return {
  11. }
  12. }
  13. }
  14. </script>

Action--异步处理

Action 类似于 mutation,不同在于:

Action 提交的是 mutation,而不是直接变更状态。 Action 可以包含任意异步操作。 mutation只能同步处理

main.js


  
  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import router from './router'
  6. //vuex使用
  7. import Vuex from 'vuex'
  8. Vue.use(Vuex)
  9. const store = new Vuex.Store({
  10. state: {
  11. //全局变量
  12. count: 0
  13. },
  14. //更改状态方法
  15. mutations: {
  16. //state为上面的state
  17. addData(state) {
  18. // 变更状态
  19. state.count++
  20. }
  21. },
  22. //过滤
  23. getters: {
  24. getState(state) {
  25. if (state.count >= 5) {
  26. return 5
  27. } else {
  28. return state.count
  29. }
  30. }
  31. },
  32. actions: {
  33. //action触发的mutations方法 优势是异步处理
  34. addData(context) {
  35. //模拟异步
  36. setTimeout(() => {
  37. context.commit('addData')
  38. }, 1000)
  39. }
  40. }
  41. })
  42. Vue.config.productionTip = false
  43. /* eslint-disable no-new */
  44. new Vue({
  45. el: '#app',
  46. router,
  47. //vuex必须加入
  48. store,
  49. components: { App },
  50. template: '<App/>'
  51. })

在发送时 应该调用action


  
  1. <template>
  2. <div>
  3. 老大有{{showData}}
  4. <HelloWorld2/>
  5. <button type = "button" v-on:click = "changeData"> 修改按钮 </button>
  6. </div>
  7. </template>
  8. <script>
  9. import HelloWorld2 from './HelloWorld2'
  10. import son from './son'
  11. export default {
  12. name: 'HelloWorld',
  13. data () {
  14. return {
  15. message2:"",
  16. }
  17. },
  18. components:{
  19. HelloWorld2,
  20. son
  21. },computed: {
  22. showData(){
  23. return this.$store.getters.getState;
  24. }
  25. },
  26. methods: {
  27. //执行更改
  28. changeData(event){
  29. //操作mutations方法
  30. //this.$store.commit("addData");
  31. //应该操作action而不是action触发的mutations方法
  32. this.$store.dispatch("addData");
  33. }
  34. }
  35. }
  36. </script>

Module

由于使用单一状态树,应用的所有状态会集中到一个比较大的对象。当应用变得非常复杂时,store 对象就有可能变得相当臃肿。

为了解决以上问题,Vuex 允许我们将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter、甚至是嵌套子模块——从上至下进行同样方式的分割:

如路由可以分割文件 不在main.js中放入vuex 新建store/index.js


  
  1. //vuex使用
  2. import Vue from 'vue'
  3. import Vuex from 'vuex'
  4. Vue.use(Vuex)
  5. export default new Vuex.Store({
  6. state: {
  7. //全局变量
  8. count: 0
  9. },
  10. //更改状态方法
  11. mutations: {
  12. //state为上面的state
  13. addData(state) {
  14. // 变更状态
  15. state.count++
  16. }
  17. },
  18. //过滤
  19. getters: {
  20. getState(state) {
  21. if (state.count >= 5) {
  22. return 5
  23. } else {
  24. return state.count
  25. }
  26. }
  27. },
  28. actions: {
  29. //action触发的mutations方法 优势是异步处理
  30. addData(context) {
  31. //模拟异步
  32. setTimeout(() => {
  33. context.commit('addData')
  34. }, 1000)
  35. }
  36. }
  37. })

修改main.js


  
  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import router from './router'
  6. import store from './store'
  7. Vue.config.productionTip = false
  8. /* eslint-disable no-new */
  9. new Vue({
  10. el: '#app',
  11. router,
  12. //vuex必须加入
  13. store,
  14. components: { App },
  15. template: '<App/>'
  16. })

我们还能把main.js中的state拿出 新建store/state.js


  
  1. export default {
  2. count: 0
  3. }

然后index.js可以改成


  
  1. //vuex使用
  2. import Vue from 'vue'
  3. import Vuex from 'vuex'
  4. import state from './state'
  5. Vue.use(Vuex)
  6. export default new Vuex.Store({
  7. state: state,
  8. //更改状态方法
  9. mutations: {
  10. //state为上面的state
  11. addData(state) {
  12. // 变更状态
  13. state.count++
  14. }
  15. },
  16. //过滤
  17. getters: {
  18. getState(state) {
  19. if (state.count >= 5) {
  20. return 5
  21. } else {
  22. return state.count
  23. }
  24. }
  25. },
  26. actions: {
  27. //action触发的mutations方法 优势是异步处理
  28. addData(context) {
  29. //模拟异步
  30. setTimeout(() => {
  31. context.commit('addData')
  32. }, 1000)
  33. }
  34. }
  35. })

 

文章来源: baocl.blog.csdn.net,作者:小黄鸡1992,版权归原作者所有,如需转载,请联系作者。

原文链接:baocl.blog.csdn.net/article/details/104730079

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。