进修Vue框架中必把握的重点常识(3)

<div> <header> <!-- 我们但愿把页头放这里 --> </header> <main> <!-- 我们但愿把主要内容放这里 --> </main> <footer> <!-- 我们但愿把页脚放这里 --> </footer> </div>

对付这样的环境,<slot> 元素有一个非凡的 attribute:name。这个 attribute 可以用来界说特另外插槽:

<div> <header> <slot></slot> </header> <main> <slot></slot> </main> <footer> <slot></slot> </footer> </div>

一个不带 name 的 <slot> 出口会带有隐含的名字“default”。

在向具名插槽提供内容的时候,我们可以在一个 <template> 元素上利用 v-slot 指令,并以 v-slot 的参数的形式提供其名称:

<base-layout> <template v-slot:header> <h1>Here might be a page title</h1> </template> <p>A paragraph for the main content.</p> <p>And another one.</p> <template v-slot:footer> <p>Here's some contact info</p> </template> </base-layout>

此刻 <template> 元素中的所有内容都将会被传入相应的插槽。任何没有被包裹在带有 v-slot 的 <template> 中的内容城市被视为默认插槽的内容。

然而,假如你但愿更明晰一些,仍然可以在一个 <template> 中包裹默认插槽的内容:

<base-layout> <template v-slot:header> <h1>Here might be a page title</h1> </template> <template v-slot:default> <p>A paragraph for the main content.</p> <p>And another one.</p> </template> <template v-slot:footer> <p>Here's some contact info</p> </template> </base-layout>

任何一种写法城市渲染出:

<div> <header> <h1>Here might be a page title</h1> </header> <main> <p>A paragraph for the main content.</p> <p>And another one.</p> </main> <footer> <p>Here's some contact info</p> </footer> </div>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsjydf.html