目的: 封装一个高复用的面包屑组件,适用于多级场景。认识 render 选项和 h 函数。
参考 element-ui 的面包屑组件
两个组件,xtx-bread 和 xtx-bread-item 才能完成动态展示
<script>
import { createVNode } from 'vue'
export default {
name: 'XtxBread',
render ({ $slots }) {
const slot = $slots.default().filter(item => item.type.name === 'XtxBreadItem' || item.type.name === 'Transition')
const arr = []
slot.forEach((item, index) => {
arr.push(item)
if (index < slot.length - 1) {
const iVNode = createVNode('i', { class: 'iconfont icon-angle-right' })
arr.push(iVNode)
}
})
const boxVNode = createVNode('div', { class: 'xtx-bread' }, arr)
return boxVNode
}
}
</script>
<style lang="less">
.xtx-bread {
display: flex;
padding: 25px 10px;
&-item {
a {
color: #666;
transition: all 0.4s;
&:hover {
color: @xtxColor;
}
}
}
i {
font-size: 12px;
margin-left: 5px;
margin-right: 5px;
line-height: 22px;
}
}
</style>
<template>
<div class="xtx-bread-item">
<RouterLink v-if="to" :to="to"><slot /></RouterLink>
<span v-else><slot /></span>
<!-- <i class="iconfont icon-angle-right"></i> -->
</div>
</template>
<script>
export default {
name: 'XtxBreadItem',
props: {
to: {
type: [String, Object]
}
}
}
</script>
首页 > 电器 > 空调
<!-- 面包屑 -->
<XtxBread>
<XtxBreadItem to="/">首页</XtxBreadItem>
<XtxBreadItem to="/category/1005000">电器</XtxBreadItem>
<XtxBreadItem >空调</XtxBreadItem>
</XtxBread>
|