VUEX的简单使用

福尔摩斯对我笑 2019-05-29 72447 次 11283条

/ state,驱动应用的数据源;
// view,以声明方式将 state 映射到视图;
// actions,响应在 view 上的用户输入导致的状态变化。


const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment: state=> state.count ++ ,
    decrement: state=> state.count--
  },
  // Action 提交的是 mutation,而不是直接变更状态。
  actions: {
    increment (context, seek){
      console.log(seek);
      context.commit('increment')
    }
  }
})



  new Vue({
    el: '#app',
    // computed : {
    //   count(){
    //     // return store.state.count
    //     // console.log(this.$store.state);
    //     return this.$store.state.count
    //   }
    // },
    //映射
    computed: new Vuex.mapState({
      count: state => state.count,
      countAlisa: 'count', //好像是别名
      countPlusLocalState (state) {
        return state.count + this.localCount
      }
    }),
    
    store,
    methods: {
      increment (){
          // store.commit('increment')
          store.dispatch('increment', 'seek') // 派发触发-》actions-》mutaions , action 内部可以执行异步
      },
      decrement (){
        store.commit('decrement')
      }
    }
  })


  // store.state 来获取状态对象
  // store.commit 方法触发状态变更
  // 再次强调,我们通过提交 mutation 的方式,而非直接改变 store.state.count,是因为我们想要更明确地追踪到状态的变化。这个简单的约定能够让你的意图更加明显,这样你在阅读代码的时候能更容易地解读应用内部的状态改变。此外,这样也让我们有机会去实现一些能记录每次状态改变,保存状态快照的调试工具。有了它,我们甚至可以实现如时间穿梭般的调试体验。


  // mapState 辅助函数 当一个组件需要获取多个状态时候,将这些状态都声明为计算属性会有些重复和冗余。为了解决这个问题,我们可以使用 mapState 辅助函数帮助我们生成计算属性,让你少按几次键:



// store.state.count= 10
store.commit('increment')
console.log(store.state.count)

See the Pen
vuex的简单使用
by SeekHappy (@Mister-weizhuang)
on CodePen.

标签: 无

非特殊说明,本博所有文章均为博主原创。

头像也是LOGO
未来博客 博主大人
到了这个年纪,不应该再彷徨!

发表评论



 


已有 11283 条评论 1


  1. golden goose

    I must express my appreciation to the writer just for rescuing me from this type of crisis. Because of looking out throughout the world wide web and meeting ways which are not beneficial, I assumed my entire life was done. Living without the presence of answers to the issues you have resolved as a result of your entire post is a crucial case, and the ones which could have in a negative way affected my entire career if I had not discovered your web page. That knowledge and kindness in controlling all things was very useful. I am not sure what I would have done if I hadn’t discovered such a point like this. I can at this time look forward to my future. Thanks very much for the expert and effective help. I will not be reluctant to propose the website to any person who should have support on this subject.

    回复 2019年10月26日
  2. yeezy shoes

    Thanks so much for giving everyone such a marvellous chance to read critical reviews from this site. It is always so great and also stuffed with a great time for me personally and my office fellow workers to search your website more than 3 times in a week to find out the newest items you have got. And definitely, I’m so at all times motivated with all the beautiful principles you give. Certain 1 tips in this posting are in fact the most suitable I have ever had.

    回复 2019年10月26日
  3. off white shoes

    I’m just commenting to make you be aware of of the exceptional experience my princess undergone using your web page. She mastered a wide variety of issues, with the inclusion of how it is like to have an ideal coaching spirit to have other individuals very easily thoroughly grasp specified extremely tough issues. You truly exceeded her expected results. Many thanks for displaying the essential, trusted, edifying as well as cool thoughts on this topic to Julie.

    回复 2019年10月26日
  4. jordan 4

    I simply wanted to say thanks once again. I do not know the things that I would have undertaken without the actual suggestions discussed by you relating to this question. Certainly was an absolute difficult concern in my view, but being able to view a well-written form you dealt with it took me to leap over joy. I’m grateful for your help and expect you recognize what a powerful job that you are accomplishing instructing the others with the aid of your blog post. I know that you’ve never got to know all of us.

    回复 2019年10月26日
  5. yeezy boost

    My husband and i have been absolutely contented that Chris could carry out his inquiry through your precious recommendations he had in your web page. It is now and again perplexing just to find yourself releasing guidelines that many most people may have been trying to sell. And we all realize we need the website owner to give thanks to because of that. The most important explanations you have made, the simple website menu, the friendships you can assist to engender – it’s got all astonishing, and it’s making our son in addition to us understand the matter is fun, which is pretty indispensable. Thanks for all!

    回复 2019年10月25日
  6. air max 95

    I simply had to appreciate you once again. I am not sure the things I might have worked on in the absence of the type of information documented by you over my question. Completely was an absolute alarming setting in my view, however , taking a look at a professional approach you treated that forced me to jump for fulfillment. Now i am happy for the support as well as hope you know what a powerful job you are always providing teaching most people by way of your web page. I am sure you haven’t met all of us.

    回复 2019年10月25日
  7. adidas superstars

    My husband and i felt so lucky when Emmanuel managed to round up his inquiry by way of the ideas he received from your blog. It is now and again perplexing to just always be offering guidance which often some others might have been trying to sell. And now we understand we have got you to give thanks to for that. The main illustrations you’ve made, the simple website navigation, the relationships you can assist to instill – it’s all awesome, and it’s aiding our son in addition to us know that the idea is amusing, which is extremely important. Thanks for the whole thing!

    回复 2019年10月25日
  8. supreme hoodie

    I needed to put you one little word to finally give thanks the moment again relating to the precious strategies you have featured at this time. It has been quite generous of people like you giving without restraint precisely what a lot of folks would’ve advertised as an ebook to help make some dough for their own end, precisely now that you could have done it if you considered necessary. The concepts additionally served as a fantastic way to know that some people have a similar eagerness similar to mine to realize good deal more with respect to this matter. I believe there are several more pleasurable times up front for people who scan through your site.

    回复 2019年10月25日
  9. yeezy wave runner 700

    I simply wanted to jot down a small comment so as to appreciate you for those fantastic tactics you are giving here. My time intensive internet investigation has finally been rewarded with professional suggestions to go over with my company. I ‘d tell you that most of us readers actually are rather blessed to exist in a great site with many special people with valuable hints. I feel very fortunate to have encountered your web page and look forward to really more enjoyable moments reading here. Thanks once again for everything.

    回复 2019年10月24日
  10. jordan shoes

    I needed to post you a very little observation so as to thank you so much over again over the beautiful pointers you have featured above. This has been quite particularly generous with you to grant without restraint what exactly a few individuals would’ve distributed for an e book in order to make some money for their own end, most notably now that you might well have done it if you wanted. Those ideas in addition acted like the fantastic way to fully grasp that other people online have the same interest just as mine to see much more around this condition. Certainly there are lots of more enjoyable periods up front for folks who looked over your blog.

    回复 2019年10月24日
  11. off white jordan 1

    I actually wanted to develop a brief comment so as to appreciate you for some of the nice instructions you are posting at this site. My time intensive internet research has at the end of the day been honored with reasonable facts to talk about with my family. I ‘d state that that we readers actually are quite blessed to exist in a remarkable community with very many special individuals with very beneficial tricks. I feel extremely fortunate to have come across the website and look forward to many more thrilling minutes reading here. Thanks once again for everything.

    回复 2019年10月24日
  12. christian louboutin shoes

    I am glad for writing to make you understand what a notable experience our girl went through studying your webblog. She mastered a lot of issues, with the inclusion of what it is like to possess a very effective teaching nature to have the others effortlessly have an understanding of some complicated matters. You truly surpassed her expected results. Thanks for imparting such beneficial, safe, explanatory not to mention unique guidance on that topic to Julie.

    回复 2019年10月24日
  13. golden goose sneakers

    I simply wished to thank you so much again. I am not sure what I would have accomplished without the type of creative ideas revealed by you over this field. It has been an absolute depressing crisis in my view, but coming across a skilled strategy you managed that took me to weep over fulfillment. I’m just happier for your work and even trust you comprehend what an amazing job you are getting into instructing men and women through your webblog. More than likely you haven’t come across all of us.

    回复 2019年10月23日
  14. hermes

    I simply desired to appreciate you once more. I am not sure the things that I would’ve handled in the absence of these basics shown by you concerning such a problem. This was an absolute hard condition in my view, but coming across a new specialised manner you resolved the issue forced me to cry over delight. I’m happy for this advice and pray you realize what an amazing job that you are providing teaching people through the use of your webblog. I am certain you haven’t met all of us.

    回复 2019年10月23日
  15. adidas nmd

    I would like to show some thanks to the writer just for rescuing me from this particular crisis. Because of scouting through the the web and obtaining strategies which were not productive, I thought my life was gone. Being alive minus the strategies to the problems you’ve sorted out all through your entire short post is a crucial case, as well as ones that would have badly damaged my entire career if I had not come across your blog. Your own know-how and kindness in maneuvering all the stuff was valuable. I don’t know what I would’ve done if I hadn’t come across such a subject like this. I can also at this moment look ahead to my future. Thank you very much for the skilled and sensible guide. I will not think twice to suggest the sites to anybody who will need tips on this matter.

    回复 2019年10月22日
  16. nike air force 1

    I want to show my respect for your generosity giving support to folks that need guidance on in this topic. Your very own commitment to getting the solution along became pretty informative and has truly made guys and women just like me to achieve their aims. Your entire important key points can mean a lot to me and far more to my peers. Many thanks; from all of us.

    回复 2019年10月21日
  17. lebron james shoes

    Thank you for all your valuable effort on this web page. My daughter take interest in engaging in investigations and it is easy to see why. Most of us notice all regarding the lively mode you produce very useful tips by means of this website and in addition increase participation from other individuals on the area plus our favorite daughter is always being taught so much. Have fun with the remaining portion of the new year. You’re the one carrying out a stunning job.

    回复 2019年10月20日
  18. fila

    I wanted to send you a little bit of word to finally give thanks again for all the stunning basics you’ve contributed on this page. It’s surprisingly open-handed with people like you in giving easily precisely what a number of people would have advertised as an ebook to help with making some dough for their own end, certainly since you might well have tried it in case you wanted. Those pointers as well served to become a great way to recognize that some people have a similar eagerness like mine to figure out much more in terms of this problem. I know there are several more pleasurable instances up front for those who read carefully your blog post.

    回复 2019年10月19日
  19. adidas yeezy

    I simply wished to thank you so much once more. I do not know the things I would’ve implemented in the absence of these tips revealed by you concerning this subject matter. Completely was an absolute alarming matter in my view, but witnessing a new specialised avenue you handled that made me to jump over fulfillment. Now i’m grateful for this work and then hope you really know what an amazing job you are accomplishing teaching men and women through the use of your web page. I am certain you have never got to know all of us.

    回复 2019年10月17日
  20. kobe sneakers

    I and also my guys happened to be looking through the good points from your site then all of the sudden came up with a terrible suspicion I had not expressed respect to the website owner for them. These women happened to be as a consequence joyful to read all of them and have without a doubt been using these things. We appreciate you genuinely indeed kind and also for deciding upon such great subjects most people are really eager to understand about. My very own sincere apologies for not expressing gratitude to earlier.

    回复 2019年10月16日
  21. nhl jerseys

    I wanted to make a brief message in order to appreciate you for all of the great hints you are sharing on this site. My incredibly long internet look up has at the end of the day been honored with really good strategies to talk about with my classmates and friends. I would suppose that most of us site visitors actually are extremely blessed to live in a really good website with very many awesome people with valuable tips and hints. I feel extremely privileged to have encountered your webpage and look forward to plenty of more exciting minutes reading here. Thank you once more for all the details.

    回复 2019年10月16日
  22. yeezy boost 350

    I’m also writing to let you be aware of of the amazing discovery our daughter gained viewing your webblog. She even learned several issues, which included what it is like to possess a wonderful coaching heart to get the rest really easily completely grasp specific very confusing matters. You really did more than visitors’ expected results. Thank you for churning out such priceless, dependable, informative and also easy tips about this topic to Sandra.

    回复 2019年10月15日
  23. balenciaga sneakers

    I actually wanted to send a quick note in order to thank you for all the amazing tactics you are sharing on this site. My incredibly long internet investigation has finally been compensated with beneficial facts and techniques to write about with my close friends. I would suppose that many of us website visitors actually are undoubtedly fortunate to be in a wonderful network with very many wonderful individuals with good basics. I feel very much lucky to have come across your entire web pages and look forward to some more fabulous times reading here. Thank you once more for a lot of things.

    回复 2019年10月14日
  24. nike epic react

    Thanks so much for giving everyone a very pleasant possiblity to check tips from this blog. It’s usually so good and as well , stuffed with amusement for me personally and my office co-workers to search your website the equivalent of three times weekly to study the latest things you have got. Not to mention, I’m just usually astounded concerning the astonishing ideas you give. Selected 3 tips in this posting are certainly the most beneficial we have ever had.

    回复 2019年10月13日
  25. golden goose

    I just wanted to send a small note so as to appreciate you for those magnificent solutions you are giving at this website. My time-consuming internet investigation has at the end been rewarded with pleasant suggestions to talk about with my relatives. I would assert that we site visitors actually are extremely endowed to dwell in a wonderful place with so many lovely people with interesting solutions. I feel quite blessed to have used your entire website and look forward to so many more amazing minutes reading here. Thank you again for a lot of things.

    回复 2019年10月12日
  26. mbt shoes

    I want to show my appreciation to the writer just for bailing me out of this setting. After surfing through the search engines and obtaining tricks that were not pleasant, I assumed my life was over. Existing minus the approaches to the issues you have resolved by means of your entire write-up is a serious case, and the ones which might have in a wrong way damaged my career if I had not noticed your web site. Your actual talents and kindness in maneuvering all the details was tremendous. I’m not sure what I would’ve done if I had not come upon such a stuff like this. I can also at this time look ahead to my future. Thanks for your time very much for your professional and results-oriented help. I will not think twice to propose your web page to anybody who requires guidance about this subject.

    回复 2019年10月11日
  27. golden goose sneakers

    I wanted to put you a bit of word in order to say thanks a lot the moment again for these stunning tricks you have contributed on this website. It’s certainly tremendously open-handed of you to grant without restraint precisely what a lot of people could have made available as an e-book to help make some money for themselves, primarily considering that you might well have done it if you desired. These smart ideas additionally acted as a easy way to understand that the rest have the identical dreams like my personal own to know the truth more and more with regard to this matter. I am sure there are some more pleasurable situations ahead for individuals that browse through your blog post.

    回复 2019年10月10日
  28. moncler outlet

    I’m just commenting to let you know what a fabulous encounter my wife’s daughter encountered browsing the blog. She figured out such a lot of things, not to mention what it’s like to possess an awesome giving mood to make others smoothly grasp selected very confusing issues. You really did more than people’s desires. Many thanks for delivering these warm and helpful, healthy, explanatory and as well as easy tips on that topic to Janet.

    回复 2019年10月9日
  29. yeezy 500

    I happen to be writing to make you know what a terrific experience our princess went through reading through your webblog. She learned a wide variety of things, including what it’s like to have a very effective coaching mindset to get other individuals without hassle have an understanding of several advanced issues. You undoubtedly did more than readers’ expected results. Many thanks for supplying those invaluable, healthy, explanatory not to mention fun thoughts on your topic to Jane.

    回复 2019年10月8日
  30. jordan shoes

    I definitely wanted to write down a note to be able to say thanks to you for some of the wonderful pointers you are giving here. My prolonged internet investigation has at the end been compensated with beneficial knowledge to exchange with my family members. I ‘d say that most of us website visitors are really blessed to be in a fabulous website with so many lovely professionals with very helpful plans. I feel truly blessed to have seen your webpages and look forward to tons of more exciting moments reading here. Thanks once more for a lot of things.

    回复 2019年10月8日
  31. 小布

    建议将源码如果能公开的话,放到github,然后在文章放个链接,对你自身也有好处 ^ – ^

    回复 2019年6月12日