在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ vue slot嵌套

vue slot嵌套

父組件中有兩個子組件:子組件1 嵌套 子組件2
且子組件1 和 子組件2 都有<slot>

結果:
1.子組件1 slot不寫name ,子組件2 slot寫 name="content",
內(nèi)容可以正常渲染。

2.子組件1 slot寫name="panel" ,子組件2 slot寫 name="content",
只能渲染子組件1的插槽內(nèi)容。

3.子組件1 和 子組件2 slot都不寫 name,
內(nèi)容可以正常渲染。

請問:為什么會出現(xiàn)這種結果。

//子組件1: panel.vue
<template>
<div>
  <slot name="panel"></slot>
</div>
</template>

//子組件2 : content.vue
<template>
<div>
  <slot name="content"></slot>
</div>
</template>

//父組件
<template>
<panel>
  <h1 slot="panel">我是panel</h1>
  <content>
    <p  slot="content">我是content</p>
  </content>
</panel>
</template>

<script>
import Panel from '@/common/panel'
import Content from '@/common/content'
</script>
回答
編輯回答
她愚我

你是組件里面嵌套組件 第一種情況 panel不是具名插槽 相當于 把整個

<h1 slot="panel">我是panel</h1>
<content>
<p slot="content">我是content</p>
</content>

插到panel div里面 你說panel組件<slot name="panel"></slot> 不寫name
<slot></slot> 那應該渲染不出來 我是panel 這個h1吧

2017年11月14日 10:32
編輯回答
莓森

覺得可能是你panel下嵌套content的問題,可能是panel下嵌套content但沒有在panel里給content一個slot,才造成這樣的問題。

  1. 子組件1 slot不寫name ,子組件2 slot寫 name="content",內(nèi)容可以正常渲染。
    原因: 子組件1不寫name,此時的slot插槽內(nèi)容為原來子組件1 slot內(nèi)容及子組件2內(nèi)容.(匿名插槽)

2.子組件1 slot寫name="panel" ,子組件2 slot寫 name="content",只能渲染子組件1的插槽內(nèi) 容。
原因:具名插槽,只會顯示對應內(nèi)容。此時slot是幫 <h1 slot="panel">我是panel</h1>占位。
3.子組件1 和 子組件2 slot都不寫 name,只能渲染子組件1的插槽內(nèi)容。
這個正常情況是都顯示,panel中的slot和情況一一樣,content中的slot按道理也能顯示對應插槽內(nèi)容。(想不通)

2018年9月23日 16:02