123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559 |
- <!DOCTYPE HTML>
- <html lang="zh-cn" >
- <head>
- <meta charset="UTF-8">
- <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
- <title>State · Vuex</title>
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- <meta name="description" content="">
- <meta name="generator" content="GitBook 3.2.2">
-
-
-
-
- <link rel="stylesheet" href="../gitbook/style.css">
-
-
-
- <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism.css">
-
-
-
- <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
-
-
-
- <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-vuejs/vue.css">
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- <meta name="HandheldFriendly" content="true"/>
- <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
- <meta name="apple-mobile-web-app-capable" content="yes">
- <meta name="apple-mobile-web-app-status-bar-style" content="black">
- <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
- <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
-
- <link rel="next" href="getters.html" />
-
-
- <link rel="prev" href="core-concepts.html" />
-
- </head>
- <body>
-
- <div class="book">
- <div class="book-summary">
-
-
- <div id="book-search-input" role="search">
- <input type="text" placeholder="輸入並搜尋" />
- </div>
-
- <nav role="navigation">
-
- <script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?zoneid=1673&serve=C6AILKT&placement=vuejs" id="_carbonads_js"></script>
- <ul class="summary">
-
-
-
-
-
-
-
- <li class="chapter " data-level="1.1" data-path="./">
-
- <a href="./">
-
-
- Introduction
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.2" >
-
- <a target="_blank" href="https://github.com/vuejs/vuex/releases">
-
-
- 更新记录
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.3" data-path="installation.html">
-
- <a href="installation.html">
-
-
- 安装
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.4" data-path="intro.html">
-
- <a href="intro.html">
-
-
- Vuex 是什么?
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.5" data-path="getting-started.html">
-
- <a href="getting-started.html">
-
-
- 开始
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.6" data-path="core-concepts.html">
-
- <a href="core-concepts.html">
-
-
- 核心概念
-
- </a>
-
-
- <ul class="articles">
-
-
- <li class="chapter active" data-level="1.6.1" data-path="state.html">
-
- <a href="state.html">
-
-
- State
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.6.2" data-path="getters.html">
-
- <a href="getters.html">
-
-
- Getter
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.6.3" data-path="mutations.html">
-
- <a href="mutations.html">
-
-
- Mutation
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.6.4" data-path="actions.html">
-
- <a href="actions.html">
-
-
- Action
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.6.5" data-path="modules.html">
-
- <a href="modules.html">
-
-
- Module
-
- </a>
-
-
- </li>
-
- </ul>
-
- </li>
-
- <li class="chapter " data-level="1.7" data-path="structure.html">
-
- <a href="structure.html">
-
-
- 项目结构
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.8" data-path="plugins.html">
-
- <a href="plugins.html">
-
-
- 插件
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.9" data-path="strict.html">
-
- <a href="strict.html">
-
-
- 严格模式
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.10" data-path="forms.html">
-
- <a href="forms.html">
-
-
- 表单处理
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.11" data-path="testing.html">
-
- <a href="testing.html">
-
-
- 测试
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.12" data-path="hot-reload.html">
-
- <a href="hot-reload.html">
-
-
- 热重载
-
- </a>
-
-
- </li>
-
- <li class="chapter " data-level="1.13" data-path="api.html">
-
- <a href="api.html">
-
-
- API 文档
-
- </a>
-
-
- </li>
-
-
- <li class="divider"></li>
- <li>
- <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
- 本書使用 GitBook 釋出
- </a>
- </li>
- </ul>
- </nav>
-
-
- </div>
- <div class="book-body">
-
- <div class="body-inner">
-
-
-
- <div class="book-header" role="navigation">
-
- <!-- Title -->
- <h1>
- <i class="fa fa-circle-o-notch fa-spin"></i>
- <a href="." >State</a>
- </h1>
- </div>
- <div class="page-wrapper" tabindex="-1" role="main">
- <div class="page-inner">
-
- <div id="book-search-results">
- <div class="search-noresults">
-
- <section class="normal markdown-section">
-
- <h1 id="state">State</h1>
- <h3 id="单一状态树">单一状态树</h3>
- <p>Vuex 使用<strong>单一状态树</strong>——是的,用一个对象就包含了全部的应用层级状态。至此它便作为一个“唯一数据源 (<a href="https://en.wikipedia.org/wiki/Single_source_of_truth" target="_blank">SSOT</a>)”而存在。这也意味着,每个应用将仅仅包含一个 store 实例。单一状态树让我们能够直接地定位任一特定的状态片段,在调试的过程中也能轻易地取得整个当前应用状态的快照。</p>
- <p>单状态树和模块化并不冲突——在后面的章节里我们会讨论如何将状态和状态变更事件分布到各个子模块中。</p>
- <h3 id="在-vue-组件中获得-vuex-状态">在 Vue 组件中获得 Vuex 状态</h3>
- <p>那么我们如何在 Vue 组件中展示状态呢?由于 Vuex 的状态存储是响应式的,从 store 实例中读取状态最简单的方法就是在<a href="https://cn.vuejs.org/guide/computed.html" target="_blank">计算属性</a>中返回某个状态:</p>
- <pre class="language-"><code class="lang-js"><span class="token comment" spellcheck="true">// 创建一个 Counter 组件</span>
- <span class="token keyword">const</span> Counter <span class="token operator">=</span> <span class="token punctuation">{</span>
- template<span class="token punctuation">:</span> <span class="token template-string"><span class="token string">`<div>{{ count }}</div>`</span></span><span class="token punctuation">,</span>
- computed<span class="token punctuation">:</span> <span class="token punctuation">{</span>
- count <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> store<span class="token punctuation">.</span>state<span class="token punctuation">.</span>count
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- </code></pre>
- <p>每当 <code>store.state.count</code> 变化的时候, 都会重新求取计算属性,并且触发更新相关联的 DOM。</p>
- <p>然而,这种模式导致组件依赖全局状态单例。在模块化的构建系统中,在每个需要使用 state 的组件中需要频繁地导入,并且在测试组件时需要模拟状态。</p>
- <p>Vuex 通过 <code>store</code> 选项,提供了一种机制将状态从根组件“注入”到每一个子组件中(需调用 <code>Vue.use(Vuex)</code>):</p>
- <pre class="language-"><code class="lang-js"><span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
- el<span class="token punctuation">:</span> <span class="token string">'#app'</span><span class="token punctuation">,</span>
- <span class="token comment" spellcheck="true">// 把 store 对象提供给 “store” 选项,这可以把 store 的实例注入所有的子组件</span>
- store<span class="token punctuation">,</span>
- components<span class="token punctuation">:</span> <span class="token punctuation">{</span> Counter <span class="token punctuation">}</span><span class="token punctuation">,</span>
- template<span class="token punctuation">:</span> <span class="token template-string"><span class="token string">`
- <div class="app">
- <counter></counter>
- </div>
- `</span></span>
- <span class="token punctuation">}</span><span class="token punctuation">)</span>
- </code></pre>
- <p>通过在根实例中注册 <code>store</code> 选项,该 store 实例会注入到根组件下的所有子组件中,且子组件能通过 <code>this.$store</code> 访问到。让我们更新下 <code>Counter</code> 的实现:</p>
- <pre class="language-"><code class="lang-js"><span class="token keyword">const</span> Counter <span class="token operator">=</span> <span class="token punctuation">{</span>
- template<span class="token punctuation">:</span> <span class="token template-string"><span class="token string">`<div>{{ count }}</div>`</span></span><span class="token punctuation">,</span>
- computed<span class="token punctuation">:</span> <span class="token punctuation">{</span>
- count <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$store<span class="token punctuation">.</span>state<span class="token punctuation">.</span>count
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span>
- </code></pre>
- <h3 id="mapstate-辅助函数"><code>mapState</code> 辅助函数</h3>
- <p>当一个组件需要获取多个状态时候,将这些状态都声明为计算属性会有些重复和冗余。为了解决这个问题,我们可以使用 <code>mapState</code> 辅助函数帮助我们生成计算属性,让你少按几次键:</p>
- <pre class="language-"><code class="lang-js"><span class="token comment" spellcheck="true">// 在单独构建的版本中辅助函数为 Vuex.mapState</span>
- <span class="token keyword">import</span> <span class="token punctuation">{</span> mapState <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vuex'</span>
- <span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
- <span class="token comment" spellcheck="true">// ...</span>
- computed<span class="token punctuation">:</span> <span class="token function">mapState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
- <span class="token comment" spellcheck="true">// 箭头函数可使代码更简练</span>
- count<span class="token punctuation">:</span> state <span class="token operator">=</span><span class="token operator">></span> state<span class="token punctuation">.</span>count<span class="token punctuation">,</span>
- <span class="token comment" spellcheck="true">// 传字符串参数 'count' 等同于 `state => state.count`</span>
- countAlias<span class="token punctuation">:</span> <span class="token string">'count'</span><span class="token punctuation">,</span>
- <span class="token comment" spellcheck="true">// 为了能够使用 `this` 获取局部状态,必须使用常规函数</span>
- countPlusLocalState <span class="token punctuation">(</span>state<span class="token punctuation">)</span> <span class="token punctuation">{</span>
- <span class="token keyword">return</span> state<span class="token punctuation">.</span>count <span class="token operator">+</span> <span class="token keyword">this</span><span class="token punctuation">.</span>localCount
- <span class="token punctuation">}</span>
- <span class="token punctuation">}</span><span class="token punctuation">)</span>
- <span class="token punctuation">}</span>
- </code></pre>
- <p>当映射的计算属性的名称与 state 的子节点名称相同时,我们也可以给 <code>mapState</code> 传一个字符串数组。</p>
- <pre class="language-"><code class="lang-js">computed<span class="token punctuation">:</span> <span class="token function">mapState</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
- <span class="token comment" spellcheck="true">// 映射 this.count 为 store.state.count</span>
- <span class="token string">'count'</span>
- <span class="token punctuation">]</span><span class="token punctuation">)</span>
- </code></pre>
- <h3 id="对象展开运算符">对象展开运算符</h3>
- <p><code>mapState</code> 函数返回的是一个对象。我们如何将它与局部计算属性混合使用呢?通常,我们需要使用一个工具函数将多个对象合并为一个,以使我们可以将最终对象传给 <code>computed</code> 属性。但是自从有了<a href="https://github.com/sebmarkbage/ecmascript-rest-spread" target="_blank">对象展开运算符</a>(现处于 ECMASCript 提案 stage-4 阶段),我们可以极大地简化写法:</p>
- <pre class="language-"><code class="lang-js">computed<span class="token punctuation">:</span> <span class="token punctuation">{</span>
- localComputed <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/* ... */</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
- <span class="token comment" spellcheck="true">// 使用对象展开运算符将此对象混入到外部对象中</span>
- <span class="token operator">...</span><span class="token function">mapState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
- <span class="token comment" spellcheck="true">// ...</span>
- <span class="token punctuation">}</span><span class="token punctuation">)</span>
- <span class="token punctuation">}</span>
- </code></pre>
- <h3 id="组件仍然保有局部状态">组件仍然保有局部状态</h3>
- <p>使用 Vuex 并不意味着你需要将<strong>所有的</strong>状态放入 Vuex。虽然将所有的状态放到 Vuex 会使状态变化更显式和易调试,但也会使代码变得冗长和不直观。如果有些状态严格属于单个组件,最好还是作为组件的局部状态。你应该根据你的应用开发需要进行权衡和确定。</p>
-
- </section>
-
- </div>
- <div class="search-results">
- <div class="has-results">
-
- <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
- <ul class="search-results-list"></ul>
-
- </div>
- <div class="no-results">
-
- <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
-
- </div>
- </div>
- </div>
- </div>
- </div>
-
- <script src="//m.servedby-buysellads.com/monetization.js" type="text/javascript"></script>
- <div class="bsa-cpc"></div>
- <script>
- (function(){
- if(typeof _bsa !== 'undefined' && _bsa) {
- _bsa.init('default', 'CKYD62QM', 'placement:vuejsorg', {
- target: '.bsa-cpc',
- align: 'horizontal',
- disable_css: 'true'
- });
- }
- })();
- </script>
- </div>
-
-
- <a href="core-concepts.html" class="navigation navigation-prev " aria-label="Previous page: 核心概念">
- <i class="fa fa-angle-left"></i>
- </a>
-
-
- <a href="getters.html" class="navigation navigation-next " aria-label="Next page: Getter">
- <i class="fa fa-angle-right"></i>
- </a>
-
-
-
- </div>
- <script>
- var gitbook = gitbook || [];
- gitbook.push(function() {
- gitbook.page.hasChanged({"page":{"title":"State","level":"1.6.1","depth":2,"next":{"title":"Getter","level":"1.6.2","depth":2,"path":"getters.md","ref":"getters.md","articles":[]},"previous":{"title":"核心概念","level":"1.6","depth":1,"path":"core-concepts.md","ref":"core-concepts.md","articles":[{"title":"State","level":"1.6.1","depth":2,"path":"state.md","ref":"state.md","articles":[]},{"title":"Getter","level":"1.6.2","depth":2,"path":"getters.md","ref":"getters.md","articles":[]},{"title":"Mutation","level":"1.6.3","depth":2,"path":"mutations.md","ref":"mutations.md","articles":[]},{"title":"Action","level":"1.6.4","depth":2,"path":"actions.md","ref":"actions.md","articles":[]},{"title":"Module","level":"1.6.5","depth":2,"path":"modules.md","ref":"modules.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":"编辑此页面","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":"zh-cn","links":{"sharing":{"facebook":false,"twitter":false}},"gitbook":"2.x.x"},"file":{"path":"state.md","mtime":"2018-04-20T00:44:03.894Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2018-04-20T00:47:55.633Z"},"basePath":".","book":{"language":"zh-cn"}});
- });
- </script>
- </div>
-
- <script src="../gitbook/gitbook.js"></script>
- <script src="../gitbook/theme.js"></script>
-
-
- <script src="../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
-
-
-
- <script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
-
-
-
- <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
-
-
-
- <script src="../gitbook/gitbook-plugin-search/search.js"></script>
-
-
-
- <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
-
-
-
- <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
-
-
-
- <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
-
-
- </body>
- </html>
|