uTools-Manuals/docs/vue/选项-生命周期钩子.html
2019-04-21 11:50:48 +08:00

147 lines
9.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h2 id="选项-生命周期钩子"><a href="#选项-生命周期钩子" class="headerlink" title="选项 / 生命周期钩子" data-scroll="">选项 / 生命周期钩子</a></h2><p class="tip">所有的生命周期钩子自动绑定 <code>this</code> 上下文到实例中,因此你可以访问数据,对属性和方法进行运算。这意味着<strong>你不能使用箭头函数来定义一个生命周期方法</strong> (例如 <code>created: () =&gt; this.fetchTodos()</code>)。这是因为箭头函数绑定了父上下文,因此 <code>this</code> 与你期待的 Vue 实例不同,<code>this.fetchTodos</code> 的行为未定义。</p>
<h3 id="beforeCreate"><a href="#beforeCreate" class="headerlink" title="beforeCreate" data-scroll="">beforeCreate</a></h3><ul>
<li><p><strong>类型</strong><code>Function</code></p>
</li>
<li><p><strong>详细</strong></p>
<p>在实例初始化之后,数据观测 (data observer) 和 event/watcher 事件配置之前被调用。</p>
</li>
<li><p><strong>参考</strong><a href="guide/instance.html#生命周期图示">生命周期图示</a></p>
</li>
</ul>
<h3 id="created"><a href="#created" class="headerlink" title="created" data-scroll="">created</a></h3><ul>
<li><p><strong>类型</strong><code>Function</code></p>
</li>
<li><p><strong>详细</strong></p>
<p>在实例创建完成后被立即调用。在这一步,实例已完成以下的配置:数据观测 (data observer)属性和方法的运算watch/event 事件回调。然而,挂载阶段还没开始,<code>$el</code> 属性目前不可见。</p>
</li>
<li><p><strong>参考</strong><a href="guide/instance.html#生命周期图示">生命周期图示</a></p>
</li>
</ul>
<h3 id="beforeMount"><a href="#beforeMount" class="headerlink" title="beforeMount" data-scroll="">beforeMount</a></h3><ul>
<li><p><strong>类型</strong><code>Function</code></p>
</li>
<li><p><strong>详细</strong></p>
<p>在挂载开始之前被调用:相关的 <code>render</code> 函数首次被调用。</p>
<p><strong>该钩子在服务器端渲染期间不被调用。</strong></p>
</li>
<li><p><strong>参考</strong><a href="guide/instance.html#生命周期图示">生命周期图示</a></p>
</li>
</ul>
<h3 id="mounted"><a href="#mounted" class="headerlink" title="mounted" data-scroll="">mounted</a></h3><ul>
<li><p><strong>类型</strong><code>Function</code></p>
</li>
<li><p><strong>详细</strong></p>
<p><code>el</code> 被新创建的 <code>vm.$el</code> 替换,并挂载到实例上去之后调用该钩子。如果 root 实例挂载了一个文档内元素,当 <code>mounted</code> 被调用时 <code>vm.$el</code> 也在文档内。</p>
<p>注意 <code>mounted</code> <strong>不会</strong>承诺所有的子组件也都一起被挂载。如果你希望等到整个视图都渲染完毕,可以用 <a href="#vm-nextTick">vm.$nextTick</a> 替换掉 <code>mounted</code></p>
<pre><code class="hljs js">mounted: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.$nextTick(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-comment">// Code that will run only after the</span>
<span class="hljs-comment">// entire view has been rendered</span>
})
}</code></pre>
<p><strong>该钩子在服务器端渲染期间不被调用。</strong></p>
</li>
<li><p><strong>参考</strong><a href="guide/instance.html#生命周期图示">生命周期图示</a></p>
</li>
</ul>
<h3 id="beforeUpdate"><a href="#beforeUpdate" class="headerlink" title="beforeUpdate" data-scroll="">beforeUpdate</a></h3><ul>
<li><p><strong>类型</strong><code>Function</code></p>
</li>
<li><p><strong>详细</strong></p>
<p>数据更新时调用,发生在虚拟 DOM 打补丁之前。这里适合在更新之前访问现有的 DOM比如手动移除已添加的事件监听器。</p>
<p><strong>该钩子在服务器端渲染期间不被调用,因为只有初次渲染会在服务端进行。</strong></p>
</li>
<li><p><strong>参考</strong><a href="guide/instance.html#生命周期图示">生命周期图示</a></p>
</li>
</ul>
<h3 id="updated"><a href="#updated" class="headerlink" title="updated" data-scroll="">updated</a></h3><ul>
<li><p><strong>类型</strong><code>Function</code></p>
</li>
<li><p><strong>详细</strong></p>
<p>由于数据更改导致的虚拟 DOM 重新渲染和打补丁,在这之后会调用该钩子。</p>
<p>当这个钩子被调用时,组件 DOM 已经更新,所以你现在可以执行依赖于 DOM 的操作。然而在大多数情况下,你应该避免在此期间更改状态。如果要相应状态改变,通常最好使用<a href="#computed">计算属性</a><a href="#watch">watcher</a> 取而代之。</p>
<p>注意 <code>updated</code> <strong>不会</strong>承诺所有的子组件也都一起被重绘。如果你希望等到整个视图都重绘完毕,可以用 <a href="#vm-nextTick">vm.$nextTick</a> 替换掉 <code>updated</code></p>
<pre><code class="hljs js">updated: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.$nextTick(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-comment">// Code that will run only after the</span>
<span class="hljs-comment">// entire view has been re-rendered</span>
})
}</code></pre>
<p><strong>该钩子在服务器端渲染期间不被调用。</strong></p>
</li>
<li><p><strong>参考</strong><a href="guide/instance.html#生命周期图示">生命周期图示</a></p>
</li>
</ul>
<h3 id="activated"><a href="#activated" class="headerlink" title="activated" data-scroll="">activated</a></h3><ul>
<li><p><strong>类型</strong><code>Function</code></p>
</li>
<li><p><strong>详细</strong></p>
<p>keep-alive 组件激活时调用。</p>
<p><strong>该钩子在服务器端渲染期间不被调用。</strong></p>
</li>
<li><p><strong>参考</strong></p>
<ul>
<li><a href="#keep-alive">构建组件 - keep-alive</a></li>
<li><a href="guide/components-dynamic-async.html#在动态组件上使用-keep-alive">动态组件 - keep-alive</a></li>
</ul>
</li>
</ul>
<h3 id="deactivated"><a href="#deactivated" class="headerlink" title="deactivated" data-scroll="">deactivated</a></h3><ul>
<li><p><strong>类型</strong><code>Function</code></p>
</li>
<li><p><strong>详细</strong></p>
<p>keep-alive 组件停用时调用。</p>
<p><strong>该钩子在服务器端渲染期间不被调用。</strong></p>
</li>
<li><p><strong>参考</strong></p>
<ul>
<li><a href="#keep-alive">构建组件 - keep-alive</a></li>
<li><a href="guide/components-dynamic-async.html#在动态组件上使用-keep-alive">动态组件 - keep-alive</a></li>
</ul>
</li>
</ul>
<h3 id="beforeDestroy"><a href="#beforeDestroy" class="headerlink" title="beforeDestroy" data-scroll="">beforeDestroy</a></h3><ul>
<li><p><strong>类型</strong><code>Function</code></p>
</li>
<li><p><strong>详细</strong></p>
<p>实例销毁之前调用。在这一步,实例仍然完全可用。</p>
<p><strong>该钩子在服务器端渲染期间不被调用。</strong></p>
</li>
<li><p><strong>参考</strong><a href="guide/instance.html#生命周期图示">生命周期图示</a></p>
</li>
</ul>
<h3 id="destroyed"><a href="#destroyed" class="headerlink" title="destroyed" data-scroll="">destroyed</a></h3><ul>
<li><p><strong>类型</strong><code>Function</code></p>
</li>
<li><p><strong>详细</strong></p>
<p>Vue 实例销毁后调用。调用后Vue 实例指示的所有东西都会解绑定,所有的事件监听器会被移除,所有的子实例也会被销毁。</p>
<p><strong>该钩子在服务器端渲染期间不被调用。</strong></p>
</li>
<li><p><strong>参考</strong><a href="guide/instance.html#生命周期图示">生命周期图示</a></p>
</li>
</ul>
<h3 id="errorCaptured"><a href="#errorCaptured" class="headerlink" title="errorCaptured" data-scroll="">errorCaptured</a></h3><blockquote>
<p>2.5.0+ 新增</p>
</blockquote>
<ul>
<li><p><strong>类型</strong><code>(err: Error, vm: Component, info: string) =&gt; ?boolean</code></p>
</li>
<li><p><strong>详细</strong></p>
<p>当捕获一个来自子孙组件的错误时被调用。此钩子会收到三个参数:错误对象、发生错误的组件实例以及一个包含错误来源信息的字符串。此钩子可以返回 <code>false</code> 以阻止该错误继续向上传播。</p>
<p class="tip">你可以在此钩子中修改组件的状态。因此在模板或渲染函数中设置其它内容的短路条件非常重要,它可以防止当一个错误被捕获时该组件进入一个无限的渲染循环。</p>
<p><strong>错误传播规则</strong></p>
<ul>
<li><p>默认情况下,如果全局的 <code>config.errorHandler</code> 被定义,所有的错误仍会发送它,因此这些错误仍然会向单一的分析服务的地方进行汇报。</p>
</li>
<li><p>如果一个组件的继承或父级从属链路中存在多个 <code>errorCaptured</code> 钩子,则它们将会被相同的错误逐个唤起。</p>
</li>
<li><p>如果此 <code>errorCaptured</code> 钩子自身抛出了一个错误,则这个新错误和原本被捕获的错误都会发送给全局的 <code>config.errorHandler</code></p>
</li>
<li><p>一个 <code>errorCaptured</code> 钩子能够返回 <code>false</code> 以阻止错误继续向上传播。本质上是说“这个错误已经被搞定了且应该被忽略”。它会阻止其它任何会被这个错误唤起的 <code>errorCaptured</code> 钩子和全局的 <code>config.errorHandler</code></p>
</li>
</ul>
</li>
</ul>