这次给大家带来为什么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><div class="aritcle_card flexRow">
<div class="artcardd flexRow">
<a class="aritcle_card_img" href="/xiazai/code/11092" title="SOPHP免费微信开源框架"><img
src="https://img.php.cn/upload/webcode/000/000/014/176492700787337.png" alt="SOPHP免费微信开源框架" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a href="/xiazai/code/11092" title="SOPHP免费微信开源框架">SOPHP免费微信开源框架</a>
<p>SOPHP是一款稳定开源的微信公众平台开发系统,也是基于weiphp开发的第一款商业系统。依托自身强大的钩子功能,她可以帮助大家快速开发出自己想要的微信功能插件,运营近两年来我们收获了上千用户与良好的口碑。作为一个开源产品,希望大家都能参与进来为SOPHP添砖加瓦,SOPHP团队一直都在致力于让SOPHP更加优秀。</p>
</div>
<a href="/xiazai/code/11092" title="SOPHP免费微信开源框架" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a>
</div>
</div>
<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>相信看了这些案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
相关阅读:









