基本使用
<button class="[btnStyle]">
<slot>按钮</slot>
</button>
export default {
name: 'MyButton',
props: ['btnStyle']
}
<MyButton btnStyle="样式名称">
</MyButton>
具名插槽
<button class="[btnStyle]">
<slot name="icon"></slot>
</button>
export default {
name: 'MyButton',
props: ['btnStyle']
}
<MyButton btnStyle="样式名称">
<template v-slot:icon>
<i class="icon-class"></i>
</template>
</MyButton>
作用域插槽
<button class="[btnStyle]">
<slot name="icon" :btnInfo="btnInfo"></slot>
</button>
export default {
name: 'MyButton',
props: ['btnStyle'],
data () {
return {
btnInfo: {
url: 'www.baidu.com'
}
}
}
}
<MyButton btnStyle="样式名称">
<template v-slot:icon="btnInfo">
<i class="icon-class"></i>
{{btnInfo.url}} //btnInfo可以拿到MyButton传出来的值
</template>
</MyButton>