uTools-Manuals/docs/vue/特殊特性.html
2019-04-21 11:50:48 +08:00

82 lines
7.4 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><h3 id="key"><a href="#key" class="headerlink" title="key" data-scroll="">key</a></h3><ul>
<li><p><strong>预期</strong><code>number | string</code></p>
<p><code>key</code> 的特殊属性主要用在 Vue 的虚拟 DOM 算法,在新旧 nodes 对比时辨识 VNodes。如果不使用 keyVue 会使用一种最大限度减少动态元素并且尽可能的尝试修复/再利用相同类型元素的算法。使用 key它会基于 key 的变化重新排列元素顺序,并且会移除 key 不存在的元素。</p>
<p>有相同父元素的子元素必须有<strong>独特的 key</strong>。重复的 key 会造成渲染错误。</p>
<p>最常见的用例是结合 <code>v-for</code></p>
<pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">"item in items"</span> <span class="hljs-attr">:key</span>=<span class="hljs-string">"item.id"</span>&gt;</span>...<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></code></pre>
<p>它也可以用于强制替换元素/组件而不是重复使用它。当你遇到如下场景时它可能会很有用:</p>
<ul>
<li>完整地触发组件的生命周期钩子</li>
<li>触发过渡</li>
</ul>
<p>例如:</p>
<pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">transition</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">:key</span>=<span class="hljs-string">"text"</span>&gt;</span>{{ text }}<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">transition</span>&gt;</span></code></pre>
<p><code>text</code> 发生改变时,<code>&lt;span&gt;</code> 会随时被更新,因此会触发过渡。</p>
</li>
</ul>
<h3 id="ref"><a href="#ref" class="headerlink" title="ref" data-scroll="">ref</a></h3><ul>
<li><p><strong>预期</strong><code>string</code></p>
<p><code>ref</code> 被用来给元素或子组件注册引用信息。引用信息将会注册在父组件的 <code>$refs</code> 对象上。如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素;如果用在子组件上,引用就指向组件实例:</p>
<pre><code class="hljs html"><span class="hljs-comment">&lt;!-- `vm.$refs.p` will be the DOM node --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"p"</span>&gt;</span>hello<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-comment">&lt;!-- `vm.$refs.child` will be the child component instance --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">child-component</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"child"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">child-component</span>&gt;</span></code></pre>
<p><code>v-for</code> 用于元素或组件的时候,引用信息将是包含 DOM 节点或组件实例的数组。</p>
<p>关于 ref 注册时间的重要说明:因为 ref 本身是作为渲染结果被创建的,在初始渲染的时候你不能访问它们 - 它们还不存在!<code>$refs</code> 也不是响应式的,因此你不应该试图用它在模板中做数据绑定。</p>
</li>
<li><p><strong>参考</strong><a href="guide/components-edge-cases.html#访问子组件实例或子元素">子组件引用</a></p>
</li>
</ul>
<h3 id="is"><a href="#is" class="headerlink" title="is" data-scroll="">is</a></h3><ul>
<li><p><strong>预期</strong><code>string | Object (组件的选项对象)</code></p>
<p>用于<a href="guide/components.html#动态组件">动态组件</a>且基于 <a href="guide/components.html#解析-DOM-模板时的注意事项">DOM 内模板的限制</a>来工作。</p>
<p>示例:</p>
<pre><code class="hljs html"><span class="hljs-comment">&lt;!-- 当 `currentView` 改变时,组件也跟着改变 --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">component</span> <span class="hljs-attr">v-bind:is</span>=<span class="hljs-string">"currentView"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">component</span>&gt;</span>
<span class="hljs-comment">&lt;!-- 这样做是有必要的,因为 `&lt;my-row&gt;` 放在一个 --&gt;</span>
<span class="hljs-comment">&lt;!-- `&lt;table&gt;` 内可能无效且被放置到外面 --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">table</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">tr</span> <span class="hljs-attr">is</span>=<span class="hljs-string">"my-row"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span></code></pre>
<p>更多的使用细节,请移步至下面的链接。</p>
</li>
<li><p><strong>See also</strong></p>
<ul>
<li><a href="guide/components.html#动态组件">动态组件</a></li>
<li><a href="guide/components.html#解析-DOM-模板时的注意事项">DOM 模板解析说明</a></li>
</ul>
</li>
</ul>
<h3 id="slot-废弃"><a href="#slot-废弃" class="headerlink" title="slot 废弃" data-scroll="">slot <sup style="color:#c92222">废弃</sup></a></h3><p><strong>推荐 2.6.0 新增的 <a href="#v-slot">v-slot</a></strong></p>
<ul>
<li><p><strong>预期</strong><code>string</code></p>
<p>用于标记往哪个具名插槽中插入子组件内容。</p>
</li>
<li><p><strong>参考</strong><a href="guide/components-slots.html#具名插槽">具名插槽</a></p>
</li>
</ul>
<h3 id="slot-scope-废弃"><a href="#slot-scope-废弃" class="headerlink" title="slot-scope 废弃" data-scroll="">slot-scope <sup style="color:#c92222">废弃</sup></a></h3><p><strong>推荐 2.6.0 新增的 <a href="#v-slot">v-slot</a></strong></p>
<ul>
<li><p><strong>预期</strong><code>function argument expression</code></p>
</li>
<li><p><strong>用法</strong></p>
<p>用于将元素或组件表示为作用域插槽。特性的值应该是可以出现在函数签名的参数位置的合法的 JavaScript 表达式。这意味着在支持的环境中,你还可以在表达式中使用 ES2015 解构。它在 2.5.0+ 中替代了 <a href="#scope-replaced"><code>scope</code></a></p>
<p>此属性不支持动态绑定。</p>
</li>
<li><p><strong>参考</strong><a href="guide/components-slots.html#作用域插槽">作用域插槽</a></p>
</li>
</ul>
<h3 id="scope-移除"><a href="#scope-移除" class="headerlink" title="scope 移除" data-scroll="">scope <sup style="color:#c92222">移除</sup></a></h3><p><strong>被 2.5.0 新增的 <a href="#slot-scope">slot-scope</a> 取代。推荐 2.6.0 新增的 <a href="#v-slot">v-slot</a></strong></p>
<p>用于表示一个作为带作用域的插槽的 <code>&lt;template&gt;</code> 元素,它在 2.5.0+ 中被 <a href="#slot-scope"><code>slot-scope</code></a> 替代。</p>
<ul>
<li><p><strong>用法:</strong></p>
<p>除了 <code>scope</code> 只可以用于 <code>&lt;template&gt;</code> 元素,其它和 <a href="#slot-scope"><code>slot-scope</code></a> 都相同。</p>
</li>
</ul>