为什么slot都是用在子组件


本文摘自PHP中文网,作者php中世界最好的语言,侵删。

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

使用slot场景一:

子组件Minput.vue

1

<input type='text'/>

 父组件 Minput

1

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

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

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

好 用slot

子组件

1

2

<input type='text'/>

<slot></slot>

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

场景二:具名插槽

子组件 he.vue

1

2

3

<header>

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

</header>

 父组件

1

2

3

<he>

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

</he>

  渲染出来的结果就是

1

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

场景三

子组件 child

1

2

3

4

<div>

    <h1>这是h1</h1>

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

</div>

父组件

1

2

3

4

<child>

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

    <p>两段p</p>

</child>

渲染出来就是

1

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

如果父组件

1

<child></child>

那么渲染出来的就是

1

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

场景四:作用域插槽

1

2

3

<div class="child">

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

</div>

父组件

1

2

3

4

5

6

7

8

<div class="parent">

  <child>

    <template slot-scope="props">

      <span>hello from parent</span>

      <span>{{ props.text }}</span>

    </template>

  </child>

</div>

x渲染的话就是

1

2

3

4

5

6

<div class="parent">

  <div class="child">

    <span>hello from parent</span>

    <span>hello from child</span>

  </div>

</div>

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

相关阅读:

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

详解浏览器渲染流程

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

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

以上就是为什么slot都是用在子组件的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

react组件拆分的重要性

html中表单组件

vue中如何正确强制组件重新渲染?(方法介绍)

react组件有哪些阶段

bootstrap的组件有哪些?

angular组件学习之浅析内容投影

react组件几种声明方式是什么

vue.js中怎么引入组件

用react完成一个图片轮播组件

react创建组件的三种方式分别是什么

更多相关阅读请进入《slot》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...