intro.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <!DOCTYPE HTML>
  2. <html lang="en" >
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  6. <title>What is Vuex? · Vuex</title>
  7. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  8. <meta name="description" content="">
  9. <meta name="generator" content="GitBook 3.2.2">
  10. <link rel="stylesheet" href="../gitbook/style.css">
  11. <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism.css">
  12. <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
  13. <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-vuejs/vue.css">
  14. <meta name="HandheldFriendly" content="true"/>
  15. <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  16. <meta name="apple-mobile-web-app-capable" content="yes">
  17. <meta name="apple-mobile-web-app-status-bar-style" content="black">
  18. <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
  19. <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
  20. <link rel="next" href="getting-started.html" />
  21. <link rel="prev" href="installation.html" />
  22. </head>
  23. <body>
  24. <div class="book">
  25. <div class="book-summary">
  26. <div id="book-search-input" role="search">
  27. <input type="text" placeholder="Type to search" />
  28. </div>
  29. <nav role="navigation">
  30. <script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?zoneid=1673&serve=C6AILKT&placement=vuejs" id="_carbonads_js"></script>
  31. <ul class="summary">
  32. <li class="chapter " data-level="1.1" data-path="./">
  33. <a href="./">
  34. Introduction
  35. </a>
  36. </li>
  37. <li class="chapter " data-level="1.2" >
  38. <a target="_blank" href="https://github.com/vuejs/vuex/releases">
  39. Release Notes
  40. </a>
  41. </li>
  42. <li class="chapter " data-level="1.3" data-path="installation.html">
  43. <a href="installation.html">
  44. Installation
  45. </a>
  46. </li>
  47. <li class="chapter active" data-level="1.4" data-path="intro.html">
  48. <a href="intro.html">
  49. What is Vuex?
  50. </a>
  51. </li>
  52. <li class="chapter " data-level="1.5" data-path="getting-started.html">
  53. <a href="getting-started.html">
  54. Getting Started
  55. </a>
  56. </li>
  57. <li class="chapter " data-level="1.6" data-path="core-concepts.html">
  58. <a href="core-concepts.html">
  59. Core Concepts
  60. </a>
  61. <ul class="articles">
  62. <li class="chapter " data-level="1.6.1" data-path="state.html">
  63. <a href="state.html">
  64. State
  65. </a>
  66. </li>
  67. <li class="chapter " data-level="1.6.2" data-path="getters.html">
  68. <a href="getters.html">
  69. Getters
  70. </a>
  71. </li>
  72. <li class="chapter " data-level="1.6.3" data-path="mutations.html">
  73. <a href="mutations.html">
  74. Mutations
  75. </a>
  76. </li>
  77. <li class="chapter " data-level="1.6.4" data-path="actions.html">
  78. <a href="actions.html">
  79. Actions
  80. </a>
  81. </li>
  82. <li class="chapter " data-level="1.6.5" data-path="modules.html">
  83. <a href="modules.html">
  84. Modules
  85. </a>
  86. </li>
  87. </ul>
  88. </li>
  89. <li class="chapter " data-level="1.7" data-path="structure.html">
  90. <a href="structure.html">
  91. Application Structure
  92. </a>
  93. </li>
  94. <li class="chapter " data-level="1.8" data-path="plugins.html">
  95. <a href="plugins.html">
  96. Plugins
  97. </a>
  98. </li>
  99. <li class="chapter " data-level="1.9" data-path="strict.html">
  100. <a href="strict.html">
  101. Strict Mode
  102. </a>
  103. </li>
  104. <li class="chapter " data-level="1.10" data-path="forms.html">
  105. <a href="forms.html">
  106. Form Handling
  107. </a>
  108. </li>
  109. <li class="chapter " data-level="1.11" data-path="testing.html">
  110. <a href="testing.html">
  111. Testing
  112. </a>
  113. </li>
  114. <li class="chapter " data-level="1.12" data-path="hot-reload.html">
  115. <a href="hot-reload.html">
  116. Hot Reloading
  117. </a>
  118. </li>
  119. <li class="chapter " data-level="1.13" data-path="api.html">
  120. <a href="api.html">
  121. API Reference
  122. </a>
  123. </li>
  124. <li class="divider"></li>
  125. <li>
  126. <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
  127. Published with GitBook
  128. </a>
  129. </li>
  130. </ul>
  131. </nav>
  132. </div>
  133. <div class="book-body">
  134. <div class="body-inner">
  135. <div class="book-header" role="navigation">
  136. <!-- Title -->
  137. <h1>
  138. <i class="fa fa-circle-o-notch fa-spin"></i>
  139. <a href="." >What is Vuex?</a>
  140. </h1>
  141. </div>
  142. <div class="page-wrapper" tabindex="-1" role="main">
  143. <div class="page-inner">
  144. <div id="book-search-results">
  145. <div class="search-noresults">
  146. <section class="normal markdown-section">
  147. <h1 id="what-is-vuex">What is Vuex?</h1>
  148. <p>Vuex is a <strong>state management pattern + library</strong> for Vue.js applications. It serves as a centralized store for all the components in an application, with rules ensuring that the state can only be mutated in a predictable fashion. It also integrates with Vue&apos;s official <a href="https://github.com/vuejs/vue-devtools" target="_blank">devtools extension</a> to provide advanced features such as zero-config time-travel debugging and state snapshot export / import.</p>
  149. <h3 id="what-is-a-state-management-pattern">What is a &quot;State Management Pattern&quot;?</h3>
  150. <p>Let&apos;s start with a simple Vue counter app:</p>
  151. <pre class="language-"><code class="lang-js"><span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  152. <span class="token comment" spellcheck="true">// state</span>
  153. data <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  154. <span class="token keyword">return</span> <span class="token punctuation">{</span>
  155. count<span class="token punctuation">:</span> <span class="token number">0</span>
  156. <span class="token punctuation">}</span>
  157. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  158. <span class="token comment" spellcheck="true">// view</span>
  159. template<span class="token punctuation">:</span> <span class="token template-string"><span class="token string">`
  160. &lt;div&gt;{{ count }}&lt;/div&gt;
  161. `</span></span><span class="token punctuation">,</span>
  162. <span class="token comment" spellcheck="true">// actions</span>
  163. methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
  164. increment <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  165. <span class="token keyword">this</span><span class="token punctuation">.</span>count<span class="token operator">++</span>
  166. <span class="token punctuation">}</span>
  167. <span class="token punctuation">}</span>
  168. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  169. </code></pre>
  170. <p>It is a self-contained app with the following parts:</p>
  171. <ul>
  172. <li>The <strong>state</strong>, which is the source of truth that drives our app;</li>
  173. <li>The <strong>view</strong>, which is just a declarative mapping of the <strong>state</strong>;</li>
  174. <li>The <strong>actions</strong>, which are the possible ways the state could change in reaction to user inputs from the <strong>view</strong>.</li>
  175. </ul>
  176. <p>This is an extremely simple representation of the concept of &quot;one-way data flow&quot;:</p>
  177. <p style="text-align: center; margin: 2em">
  178. <img style="width:100%;max-width:450px;" src="images/flow.png">
  179. </p>
  180. <p>However, the simplicity quickly breaks down when we have <strong>multiple components that share common state</strong>:</p>
  181. <ul>
  182. <li>Multiple views may depend on the same piece of state.</li>
  183. <li>Actions from different views may need to mutate the same piece of state.</li>
  184. </ul>
  185. <p>For problem one, passing props can be tedious for deeply nested components, and simply doesn&apos;t work for sibling components. For problem two, we often find ourselves resorting to solutions such as reaching for direct parent/child instance references or trying to mutate and synchronize multiple copies of the state via events. Both of these patterns are brittle and quickly lead to unmaintainable code.</p>
  186. <p>So why don&apos;t we extract the shared state out of the components, and manage it in a global singleton? With this, our component tree becomes a big &quot;view&quot;, and any component can access the state or trigger actions, no matter where they are in the tree!</p>
  187. <p>In addition, by defining and separating the concepts involved in state management and enforcing certain rules, we also give our code more structure and maintainability.</p>
  188. <p>This is the basic idea behind Vuex, inspired by <a href="https://facebook.github.io/flux/docs/overview.html" target="_blank">Flux</a>, <a href="http://redux.js.org/" target="_blank">Redux</a> and <a href="https://guide.elm-lang.org/architecture/" target="_blank">The Elm Architecture</a>. Unlike the other patterns, Vuex is also a library implementation tailored specifically for Vue.js to take advantage of its granular reactivity system for efficient updates.</p>
  189. <p><img src="images/vuex.png" alt="vuex"></p>
  190. <h3 id="when-should-i-use-it">When Should I Use It?</h3>
  191. <p>Although Vuex helps us deal with shared state management, it also comes with the cost of more concepts and boilerplate. It&apos;s a trade-off between short term and long term productivity.</p>
  192. <p>If you&apos;ve never built a large-scale SPA and jump right into Vuex, it may feel verbose and daunting. That&apos;s perfectly normal - if your app is simple, you will most likely be fine without Vuex. A simple <a href="https://vuejs.org/v2/guide/components.html#Non-Parent-Child-Communication" target="_blank">global event bus</a> may be all you need. But if you are building a medium-to-large-scale SPA, chances are you have run into situations that make you think about how to better handle state outside of your Vue components, and Vuex will be the natural next step for you. There&apos;s a good quote from Dan Abramov, the author of Redux:</p>
  193. <blockquote>
  194. <p>Flux libraries are like glasses: you&#x2019;ll know when you need them.</p>
  195. </blockquote>
  196. </section>
  197. </div>
  198. <div class="search-results">
  199. <div class="has-results">
  200. <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
  201. <ul class="search-results-list"></ul>
  202. </div>
  203. <div class="no-results">
  204. <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
  205. </div>
  206. </div>
  207. </div>
  208. </div>
  209. </div>
  210. <script src="//m.servedby-buysellads.com/monetization.js" type="text/javascript"></script>
  211. <div class="bsa-cpc"></div>
  212. <script>
  213. (function(){
  214. if(typeof _bsa !== 'undefined' && _bsa) {
  215. _bsa.init('default', 'CKYD62QM', 'placement:vuejsorg', {
  216. target: '.bsa-cpc',
  217. align: 'horizontal',
  218. disable_css: 'true'
  219. });
  220. }
  221. })();
  222. </script>
  223. </div>
  224. <a href="installation.html" class="navigation navigation-prev " aria-label="Previous page: Installation">
  225. <i class="fa fa-angle-left"></i>
  226. </a>
  227. <a href="getting-started.html" class="navigation navigation-next " aria-label="Next page: Getting Started">
  228. <i class="fa fa-angle-right"></i>
  229. </a>
  230. </div>
  231. <script>
  232. var gitbook = gitbook || [];
  233. gitbook.push(function() {
  234. gitbook.page.hasChanged({"page":{"title":"What is Vuex?","level":"1.4","depth":1,"next":{"title":"Getting Started","level":"1.5","depth":1,"path":"getting-started.md","ref":"getting-started.md","articles":[]},"previous":{"title":"Installation","level":"1.3","depth":1,"path":"installation.md","ref":"installation.md","articles":[]},"dir":"ltr"},"config":{"plugins":["edit-link","prism","-highlight","github","-highlight","github"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"edit-link":{"label":"Edit This Page","base":"https://github.com/vuejs/vuex/tree/dev/docs"},"github":{"url":"https://github.com/vuejs/vuex/"},"prism":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":false,"twitter":false,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-vuejs":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Vuex","language":"en","links":{"sharing":{"facebook":false,"twitter":false}},"gitbook":"2.x.x"},"file":{"path":"intro.md","mtime":"2018-04-20T00:44:03.851Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2018-04-20T00:47:55.633Z"},"basePath":".","book":{"language":"en"}});
  235. });
  236. </script>
  237. </div>
  238. <script src="../gitbook/gitbook.js"></script>
  239. <script src="../gitbook/theme.js"></script>
  240. <script src="../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
  241. <script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
  242. <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
  243. <script src="../gitbook/gitbook-plugin-search/search.js"></script>
  244. <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
  245. <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
  246. <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
  247. </body>
  248. </html>