关于slot
个人理解总结:
是决定子组件在父组件如何显示的作用
应用业务场景:
同一个组件在不同的组件中被引用,需要显示的内容不同,就可以用到slot
具名插槽
在子组件中定义了三个slot标签,其中有两个分别添加了name属性header和footer
<template>
<div class="slottwo">
<div>slottwo</div>
<slot name="header"></slot>
<slot></slot>
<slot name="footer"></slot>
</div>
</template>
在父组件中使用template并写入对应的slot值来指定该内容在子组件中现实的位置(当然也不用必须写到template),没有对应值的其他内容会被放到子组件中没有添加name属性的slot中
<template>
<div>
我是父组件
<slot-two>
<p>啦啦啦,啦啦啦,我是卖报的小行家</p>
<template slot="header">
<p>我是name为header的slot</p>
</template>
<p slot="footer">我是name为footer的slot</p>
</slot-two>
</div>
</template>
插槽的默认内容
父组件
<template>
<div>
我是父组件
<slot-two></slot-two>
</div>
</template>
子组件
<template>
<div class="slottwo">
<slot>我不是卖报的小行家</slot>
</div>
</template>
显示:
我是父组件
我不是卖报的小行家
可以在子组件的slot标签中写入内容,当父组件没有写入内容时会显示子组件的默认内容,当父组件写入内容时,会替换子组件的默认内容
作用域插槽
其实就是子组件的数据内容通过父组件展示,也就是说子组件往父组件传递数据,但是vue子传父数据有两种方式,vuex和事件传递,为什么要用到作用域插槽来传递呢?
组件在只有自己的子组件会使用自己提供的数据时,通常出现在遍历渲染时,此时用作用域插槽会变很多
案例:
<template>
<A>
<B slot-scope='aa' v-bind='aa'>
</A>
</template>
子组件
在子组件的slot标签上绑定需要的值
<template>
<div>
我是作用域插槽的子组件
<slot :data="user"></slot>
</div>
</template>
<script>
export default {
name: 'slotthree',
data () {
return {
user: [
{name: 'Jack', sex: 'boy'},
{name: 'Jone', sex: 'girl'},
{name: 'Tom', sex: 'boy'}
]
}
}
}
</script>
父组件
在父组件上使用slot-scope属性,user.data就是子组件传过来的值
<template>
<div>
我是作用域插槽
<slot-three>
<template slot-scope="user">
<div v-for="(item, index) in user.data" :key="index">
{{item}}
</div>
</template>
</slot-three>
</div>
</template>
slot使用的业务场景
1.根据用户不同的点击行为输出相应的提示信息
用vue自定义了一个组件,要根据用户不同的点击行为输出相应的提示信息,这时候用solt就最好了
<template>
<div>
<button @click="doSomething">危险</button>
<button @click="doSomething2">爱我</button>
<button @click="doSomething3">错误</button>
<son v-show="isShow">
{{show}}
</son>
</div>
</template>
<script>
import son from '@/components/son'
export default {
name: 'father',
components: {
son
},
data () {
return {
isShow:true,
show:'',
msg: 'Welcome to Your Vue.js App'
}
},
methods:{
doSomething(){
this.show = '您在进行危险操作'
},
doSomething2(){
this.show = '你确定不爱我了吗'
},
doSomething3(){
this.show = '你这样做是错的'
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
<template>
<div class="son">
<slot>我不是卖报的小行家</slot>
</div>
</template>
<script>
export default {
name: 'son',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
2.slot 可以实现在已经定义的组件中添加内容,组件会接收内容并输出,假如有一个组件person,它的里面包含的是个人信息,如下面这样
<template id="per">
<div>
<p>姓名:...</p>
<p>年龄:...</p>
<p>职业:...</p>
</div>
</template>
在应用的时候,当然希望这里面可以是灵活变化的,所以这就需要用到slot了
展示父组件
<template>
<div>
<sonname >
<span slot="name">{{componentData.name}}</span>
<span slot="age">{{componentData.age}}</span>
<span slot="job">{{componentData.job}}</span>
</sonname>
</div>
</template>
<script>
import sonname from '@/components/sonname'
export default {
name: 'fathername',
isShow:true,
components: {
sonname
},
data () {
return {
componentData: {
name : "vam",
age : 18,
job : "student"
},
msg: 'Welcome to Your Vue.js App'
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
<template >
<div id="sonname">
<p>姓名:<slot name="name"></slot></p>
<p>年龄:<slot name="age"></slot></p>
<p>职业:<slot name="job"></slot></p>
</div>
</template>
<script>
export default {
name: 'sonname',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
展示效果