温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

slot都是用在子组件的原因

发布时间:2020-10-28 14:23:58 来源:亿速云 阅读:134 作者:小新 栏目:web开发

这篇文章给大家分享的是有关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>
    <h2 name='header'>hello world</h2>
</he>

  渲染出来的结果就是

<header><h2>hello world</h2></header>

场景三

子组件 child

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

父组件

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

渲染出来就是

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

如果父组件

<child></child>

那么渲染出来的就是

<div><h2>这是h2</h2>这是分发内容,只有在没有分发内容的情况下显示</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>

感谢各位的阅读!关于slot都是用在子组件的原因就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI