经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
vue--vuex详解
来源:jb51  时间:2019/4/16 8:35:26  对本文有异议

Vuex

什么是Vuex?

官方说法:Vuex 是一个专为 Vue.js应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。

个人理解:Vuex是用来管理组件之间通信的一个插件

为什么要用Vuex?

我们知道组件之间是独立的,组件之间想要实现通信,我目前知道的就只有props选项,但这也仅限于父组件和子组件之间的通信。如果兄弟组件之间想要实现通信呢?嗯..,方法应该有。抛开怎么实现的问题,试想一下,当做中大型项目时,面对一大堆组件之间的通信,还有一大堆的逻辑代码,会不会很抓狂??那为何不把组件之间共享的数据给“拎”出来,在一定的规则下管理这些数据呢? 这就是Vuex的基本思想了。

Vuex有什么特性?

怎么使用Vuex?

引入Vuex.js文件

创建实例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <script src="./js/vuex.js"></script>
  10. <script src="./js/vue2.0.js"></script>
  11. <body>
  12. <div id="app">
  13. </div>
  14. </body>
  15. <script>
  16. Vue.use(Vuex);//在创建Vue实例之前
  17. var myStore = new Vuex.Store({
  18. state:{
  19. //存放组件之间共享的数据
  20. name:"jjk"
  21. },
  22. mutations:{
  23. //显式的更改state里的数据
  24. },
  25. getters:{
  26. //获取数据的方法
  27. },
  28. actions:{
  29. //
  30. }
  31. });
  32. new Vue({
  33. el:"#app",
  34. data:{
  35. name:"dk"
  36. },
  37. store:myStore,
  38. mounted:function(){
  39. console.log(this)//控制台
  40. }
  41. })
  42. </script>
  43. </html>

先解释上面代码的意思:

new Vuex.Store({}) 表示创建一个Vuex实例,通常情况下,他需要注入到Vue实例里. Store是Vuex的一个核心方法,字面上理解为“仓库”的意思。Vuex Store是响应式的,当Vue组件从store中读取状态(state选项)时,若store中的状态发生更新时,他会及时的响应给其他的组件(类似双向数据绑定) 而且不能直接改变store的状态,改变状态的唯一方法就是,显式地提交更改(mutations选项)

他有4个核心选项:state mutations getters actions (下文会仔细分析)

这是上面代码:

那如何获取到state的数据呢?

一般会在组件的计算属性(computed)获取state的数据(因为,计算属性会监控数据变化,一旦发生改变就会响应)

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <script src="./js/vuex.js"></script>
  10. <script src="./js/vue2.0.js"></script>
  11. <body>
  12. <div id="app">
  13. <hello></hello>
  14. </div>
  15. </body>
  16. <script>
  17. Vue.use(Vuex);
  18. var myStore = new Vuex.Store({
  19. state:{
  20. //存放组件之间共享的数据
  21. name:"jjk"
  22. },
  23. mutations:{
  24. //显式的更改state里的数据
  25. },
  26. getters:{
  27. //过滤state数据
  28. },
  29. actions:{
  30. //
  31. }
  32. });
  33.  
  34. Vue.component('hello',{
  35. template:"<p>{{name}}</p>",
  36. computed: {
  37. name:function(){
  38. return this.$store.state.name
  39. }
  40. },
  41. mounted:function(){
  42. console.log(this)
  43. }
  44. })
  45. new Vue({
  46. el:"#app",
  47. data:{
  48. name:"dk"
  49. },
  50. store:myStore,
  51. mounted:function(){
  52. console.log(this)
  53. }
  54. })
  55. </script>
  56. </html>

在·chrome中显示:

  

state:用来存放组件之间共享的数据。他跟组件的data选项类似,只不过data选项是用来存放组件的私有数据。

getters:有时候,我们需要对state的数据进行筛选,过滤。这些操作都是在组件的计算属性进行的。如果多个组件需要用到筛选后的数据,那我们就必须到处重复写该计算属性函数;或者将其提取到一个公共的工具函数中,并将公共函数多处导入 - 两者都不太理想。如果把数据筛选完在传到计算属性里就不用那么麻烦了,getters就是干这个的,你可以把getters看成是store的计算属性。getters下的函数接收接收state作为第一个参数。那么,组件是如何获取经过getters过滤的数据呢? 过滤的数据会存放到$store.getters对象中。具体看一个例子:    

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <script src="./js/vuex.js"></script>
  10. <script src="./js/vue2.0.js"></script>
  11. <body>
  12. <div id="app">
  13. <hello></hello>
  14. </div>
  15. </body>
  16. <script>
  17. Vue.use(Vuex);
  18. var myStore = new Vuex.Store({
  19. state:{
  20. //存放组件之间共享的数据
  21. name:"jjk",
  22. age:18
  23. },
  24. mutations:{
  25. //显式的更改state里的数据
  26. },
  27. getters:{
  28. getAge:function(state){
  29. return state.age;
  30. }
  31. },
  32. actions:{
  33. //
  34. }
  35. });
  36.  
  37. Vue.component('hello',{
  38. template:"<p>姓名:{{name}} 年龄:{{age}}</p>",
  39. computed: {
  40. name:function(){
  41. return this.$store.state.name
  42. },
  43. age:function(){
  44. return this.$store.getters.getAge
  45. }
  46. },
  47. mounted:function(){
  48. console.log(this)
  49. }
  50. })
  51. new Vue({
  52. el:"#app",
  53. data:{
  54. name:"dk"
  55. },
  56. store:myStore,
  57. mounted:function(){
  58. console.log(this)
  59. }
  60. })
  61. </script>
  62. </html>

在chrome中显示:

 

mutations:前面讲到的都是如何获取state的数据,那如何把数据存储到state中呢?在 Vuex store 中,实际改变 状态(state) 的唯一方式是通过 提交(commit) 一个 mutation。  mutations下的函数接收state作为参数,接收一个叫做payload(载荷)的东东作为第二个参数,这个东东是用来记录开发者使用该函数的一些信息,比如说提交了什么,提交的东西是用来干什么的,包含多个字段,所以载荷一般是对象(其实这个东西跟git的commit很类似)还有一点需要注意:mutations方法必须是同步方法!  具体看实例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <script src="./js/vuex.js"></script>
  10. <script src="./js/vue2.0.js"></script>
  11. <body>
  12. <div id="app">
  13. <hello></hello>
  14. </div>
  15. </body>
  16. <script>
  17. Vue.use(Vuex);
  18. var myStore = new Vuex.Store({
  19. state:{
  20. //存放组件之间共享的数据
  21. name:"jjk",
  22. age:18,
  23. num:1
  24. },
  25. mutations:{
  26. //显式的更改state里的数据
  27. change:function(state,a){
  28. // state.num++;
  29. console.log(state.num += a);
  30. }
  31. },
  32. getters:{
  33. getAge:function(state){
  34. return state.age;
  35. }
  36. },
  37. actions:{
  38. //
  39. }
  40. });
  41.  
  42. Vue.component('hello',{
  43. template:"<p @click='changeNum'>姓名:{{name}} 年龄:{{age}} 次数:{{num}}</p>",
  44. computed: {
  45. name:function(){
  46. return this.$store.state.name
  47. },
  48. age:function(){
  49. return this.$store.getters.getAge
  50. },
  51. num:function(){
  52. return this.$store.state.num
  53. }
  54. },
  55. mounted:function(){
  56. console.log(this)
  57. },
  58. methods: {
  59. changeNum: function(){
  60. //在组件里提交
  61. // this.num++;
  62. this.$store.commit('change',10)
  63. }
  64. },
  65. data:function(){
  66. return {
  67. // num:5
  68. }
  69. }
  70. })
  71. new Vue({
  72. el:"#app",
  73. data:{
  74. name:"dk"
  75. },
  76. store:myStore,
  77. mounted:function(){
  78. console.log(this)
  79. }
  80. })
  81. </script>
  82. </html>

当点击p标签前,chrome中显示:

点击p标签后:

可以看出:更改state的数据并显示在组件中,有几个步骤:1. 在mutations选项里,注册函数 函数里面装逻辑代码。2.在组件里,this.$store.commit('change',payload) 注意:提交的函数名要一一对应 3.触发函数,state就会相应更改 4.在组件的计算属性里this.$store.state 获取你想要得到的数据

actions:既然mutations只能处理同步函数,我大js全靠‘异步回调'吃饭,怎么能没有异步,于是actions出现了...

actions和mutations的区别

1.Actions提交的是 mutations,而不是直接变更状态。也就是说,actions会通过mutations,让mutations帮他提交数据的变更。

2.Action 可以包含任意异步操作。ajax、setTimeout、setInterval不在话下   

再来看一下实例:   

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <script src="./js/vuex.js"></script>
  10. <script src="./js/vue2.0.js"></script>
  11. <body>
  12. <div id="app">
  13. <hello></hello>
  14. </div>
  15. </body>
  16. <script>
  17. Vue.use(Vuex);
  18. var myStore = new Vuex.Store({
  19. state:{
  20. //存放组件之间共享的数据
  21. name:"jjk",
  22. age:18,
  23. num:1
  24. },
  25. mutations:{
  26. //显式的更改state里的数据
  27. change:function(state,a){
  28. // state.num++;
  29. console.log(state.num += a);
  30. },
  31. changeAsync:function(state,a){
  32. console.log(state.num +=a);
  33. }
  34. },
  35. getters:{
  36. getAge:function(state){
  37. return state.age;
  38. }
  39. },
  40. actions:{
  41.         //设置延时
  42. add:function(context,value){
  43. setTimeout(function(){
  44.            //提交事件
  45. context.commit('changeAsync',value);
  46. },1000)
  47. }
  48. }
  49. });
  50.  
  51. Vue.component('hello',{
  52. template:`
  53. <div>
  54. <p @click='changeNum'>姓名:{{name}} 年龄:{{age}} 次数:{{num}}</p>
  55. <button @click='changeNumAnsyc'>change</button>
  56. </div>`,
  57. computed: {
  58. name:function(){
  59. return this.$store.state.name
  60. },
  61. age:function(){
  62. return this.$store.getters.getAge
  63. },
  64. num:function(){
  65. return this.$store.state.num
  66. }
  67. },
  68. mounted:function(){
  69. console.log(this)
  70. },
  71. methods: {
  72. changeNum: function(){
  73. //在组件里提交
  74. // this.num++;
  75. this.$store.commit('change',10)
  76. },
  77.         //在组件里派发事件 当点击按钮时,changeNumAnsyc触发-->actions里的add函数被触发-->mutations里的changeAsync函数触发
  78.  
  79.  
  80. changeNumAnsyc:function(){
  81. this.$store.dispatch('add', 5);
  82. }
  83. },
  84. data:function(){
  85. return {
  86. // num:5
  87. }
  88. }
  89. })
  90. new Vue({
  91. el:"#app",
  92. data:{
  93. name:"dk"
  94. },
  95. store:myStore,
  96. mounted:function(){
  97. console.log(this)
  98. }
  99. })
  100. </script>
  101. </html>

点击按钮一秒后,chrome中显示:

  

先整明白 context dispatch是什么东西:

context:context是与 store 实例具有相同方法和属性的对象。可以通过context.statecontext.getters来获取 state 和 getters。

dispatch:翻译为‘派发、派遣'的意思,触发事件时,dispatch就会通知actions(跟commit一样一样的)参数跟commit也是一样的。

action的大体流程:

1.在actions选项里添加函数(异步)并提交到对应的函数(在mutation选项里)中context.commit('changeAsync',value);   

  1. actions:{
  2. add:function(context,value){
  3. setTimeout(function(){
  4. context.commit('changeAsync',value);
  5. },1000)
  6. }
  7. }

2.在组件里:changeNumAnsyc:function(){this.$store.dispatch('add', 5);} 将dispatch“指向”actions选项里的函数

3. 在mutations选项里,要有对应的函数changeAsync:function(state,a){console.log(state.num +=a);}

总结:

各个类型的 API各司其职,mutation 只管存,你给我(dispatch)我就存;action只管中间处理,处理完我就给你,你怎么存我不管;Getter 我只管取,我不改的。 action放在了 methods 里面,说明我们应该把它当成函数来用(讲道理,钩子函数也应该可以的) mutation是写在store里面的,这说明,它就是个半成品,中间量,我们不应该在外面去操作它。getter写在了 computed 里面,这说明虽然 getter我们写的是函数,但是我们应该把它当成计算属性来用。

对Vuex的了解就先到这了,细节以后在补充。。。。。待续

以上所述是小编给大家介绍的vue--vuex详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对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号