首页 / VUE / vue(17)组件插槽slot的使用
vue(17)组件插槽slot的使用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了vue(17)组件插槽slot的使用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3466字,纯文字阅读大概需要5分钟。
内容图文
![vue(17)组件插槽slot的使用](/upload/InfoBanner/zyjiaocheng/988/7ebdd652aff046d18a81e5d8c51cf589.jpg)
如果有两个组件他们有百分之八十的内容是一样的,但是有百分之二十的内容是不一样的,我们首先当然可以将他们各自声明为一个组件,分别引用。
但是为了防止写重复的内容,我们可以声明一个组件,然后再这个组件中声明一个插槽,这个插槽中的内容由父组件进行填充。这样只需要声明一个子组件包括那百分之八十的内容,然后留下一个插槽,然后父组件在使用这个子组件的时候自己将不同的那百分之二十填充到插槽中即可。
子组件:
<template> <div> ????这是子组件 ????<slot></slot> </div> </template><script> export?default?({ ????data:function(){ ????????return{ ??????????? ????????} ????}, ????methods:{ ??????? ????} }) </script>
<style?scoped>
</style> ? 父组件: <template> ????<div>
??????<HelloWorld?> ????????<button>子组件1</button> ??????</HelloWorld> ??????<HelloWorld?> ????????<a?href="">子组件2</a> ??????</HelloWorld> ????</div> </template>
<script> import?HelloWorld?from?'./component/HelloWorld'
export?default?{ ???name:"App", ???data:function(){ ???????return?{ ???????? ???????}; ???}, ???components:{ ???????HelloWorld ???}, ???computed:{ ??????? ???}, ???methods:{ ?????? ???} } </script>
<style?scoped> </style> 效果:
![vue(17)组件插槽slot的使用 - 文章图片](/upload/getfiles/0001/2021/5/14/20210514034507930.jpg)
<script> export?default?({ ????data:function(){ ????????return{ ??????????? ????????} ????}, ????methods:{ ??????? ????} }) </script>
<style?scoped>
</style> ? 父组件: <template> ????<div>
??????<HelloWorld?> ????????<template?v-slot:oneSlot>//为第一个插槽附上内容 ???????????<button>第一个插槽</button> ????????</template> ???????<template?v-slot:twoSlot>//为第二个插槽附上内容 ???????????<a?href="">第二个插槽</a> ????????</template> ??????</HelloWorld> ????</div> </template>
<script> import?HelloWorld?from?'./component/HelloWorld'
export?default?{ ???name:"App", ???data:function(){ ???????return?{ ???????? ???????}; ???}, ???components:{ ???????HelloWorld ???}, ???computed:{ ??????? ???}, ???methods:{ ?????? ???} } </script>
<style?scoped> </style> ? 效果:
![vue(17)组件插槽slot的使用 - 文章图片](/upload/getfiles/0001/2021/5/14/20210514034508055.jpg)
<script> export?default?({ ????data:function(){ ????????return{ ???????????childName:"child" ????????} ????}, ????methods:{ ??????? ????} }) </script>
<style?scoped>
</style> ? 父组件: <template> ????<div>
??????<HelloWorld?> ????????<template?v-slot:oneSlot="childPro">//这个childPro包括子组件中所有定义的属性 ??????????{{parentName}}//这个parentName是父组件中的变量 ??????????<br> ??????????{{childPro.childName}}//获取子组件中属性childName的值 ????????</template>
??????</HelloWorld> ????</div> </template>
<script> import?HelloWorld?from?'./component/HelloWorld'
export?default?{ ???name:"App", ???data:function(){ ???????return?{ ????????parentName:'parent' ???????}; ???}, ???components:{ ???????HelloWorld ???}, ???computed:{ ??????? ???}, ???methods:{ ?????? ???} } </script>
<style?scoped> </style>
内容总结
以上是互联网集市为您收集整理的vue(17)组件插槽slot的使用全部内容,希望文章能够帮你解决vue(17)组件插槽slot的使用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。