好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

为什么slot都是用在子组件

这次给大家带来为什么slot都是用在子组件,使用slot子组件的注意事项有哪些,下面就是实战案例,一起来看一下。

使用slot场景一:

子组件Minput.vue

<input type='text'/> 

 父组件 Minput

<Minput>可以显示吗</Minput> 

 这种情况下 Minput标签内的文字是不会渲染出来的

如果现在想在里面把文字渲染出来怎么办

好 用slot

子组件

<input type='text'/>
<slot></slot> 

 这样的话,父组件的里面的文字就可以渲染出来

场景二:具名插槽

子组件 he.vue

<header>
    <slot name='header'></slot>
</header> 

 父组件

<he>
    <h1 name='header'>hello world</h1>
</he> 

  渲染出来的结果就是

<header><h1>hello world</h1></header> 

场景三

子组件 child

<div>
    <h1>这是h1</h1>
    <slot>这是分发内容,只有在没有分发内容的情况下显示</slot>
</div> 

父组件

<child>
    <p>这是一段p</p>
    <p>两段p</p>
</child> 

渲染出来就是

<div><h1>这是h1</h1><p>这是一段p</p><p>两段p</p></div> 

如果父组件

<child></child> 

那么渲染出来的就是

<div><h1>这是h1</h1>这是分发内容,只有在没有分发内容的情况下显示</div> 

场景四:作用域插槽

<div class="child">
  <slot text="hello from child"></slot>
</div> 

父组件

<div class="parent">
  <child>
    <template slot-scope="props">
      <span>hello from parent</span>
      <span>{{ props.text }}</span>
    </template>
  </child>
</div> 

x渲染的话就是

<div class="parent">
  <div class="child">
    <span>hello from parent</span>
    <span>hello from child</span>
  </div>
</div> 

相信看了这些案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

相关阅读:

怎样让按钮点击后出现“点”的边框

详解浏览器渲染流程

input的文本框怎么做到和img验证码

常用input文本框内容自动垂直居中并默认提示文字单击为空

以上就是为什么slot都是用在子组件的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于为什么slot都是用在子组件的详细内容...

  阅读:40次