Encapsulate your own breadcrumb component
Element-ui breadcrumb structure
<el-breadcrumb separator="/">
<el-breadcrumb-item :to="{ path: '/' }">Home page</el-breadcrumb-item>
<el-breadcrumb-item><a href="/">Activity management</a></el-breadcrumb-item>
<el-breadcrumb-item>List of activities</el-breadcrumb-item>
<el-breadcrumb-item>Event details</el-breadcrumb-item>
</el-breadcrumb>
Copy the code
# Self-encapsulating breadcrumb component
Encapsulate the base components firstmy-bread-item.vue
<template>
<div class="my-bread-item">// If there is a router-link tag, wrap it with the span tag<RouterLink v-if="to" :to="to"><slot /></RouterLink>
<span v-else><slot /></span>
</div>
</template>
<script>
export default {
name: 'MyBreadItem'.props: {
to: {
type: [String.Object]}}}</script>
Copy the code
Encapsulate the parent container componentmy-bread.vue
<script>
import { h } from 'vue'
export default {
name: 'MyBread'.render() {
// render render option, which is a function that returns the default function (h) of createElement, which is used to create the structure
// The render function returns render as component content. It has a higher priority
// Vue2.0 h functions are imported, vue3.0 h functions are imported
// 1. Create the parent container of my-bread
// 2. Obtain the content of the default slot
// 3. Remove the I tag from the my-bread-item component because it is organized by the render function
// 4. Create a dynamically created node by traversing the item in the slot
// 5. Render the dynamically created node into the my-bread tag
const items = this.$slots.default().filter(item= > item.type.name === 'MyBrandItem')
const context = []
items.forEach((item, i) = > {
context.push(item)
if (i < items.length - 1) {
context.push(h('i', { class: 'iconfont icon-angle-right' }, null))}})return h('div', { class: 'My-bread' }, context)
}
}
</script>
<style scoped lang="less">
.My-bread {
display: flex;
padding: 25px 10px;
:deep(&-item) {
a {
color: # 666;
transition: all 0.4 s;
&:hover {
color: @MyColor; }}} :deep(i) {
font-size: 12px;
margin-left: 5px;
margin-right: 5px;
line-height: 22px; }}</style>
Copy the code
To the parent container componentmy-bread.vue
JSX optimization
<script>
export default {
name: 'MyBread'.render() {
const items = this.$slots.default().filter(item= > item.type.name === 'MyBrandItem')
const context = []
items.forEach((item, i) = > {
context.push(item)
if (i < items.length - 1) {
context.push(<i className='iconfont icon-angle-right'></i>)}})return <div className='my-bread'>{results}</div>
}
}
</script>
<style scoped lang="less">
.My-bread {
display: flex;
padding: 25px 10px;
:deep(&-item) {
a {
color: # 666;
transition: all 0.4 s;
&:hover {
color: @MyColor; }}} :deep(i) {
font-size: 12px;
margin-left: 5px;
margin-right: 5px;
line-height: 22px; }}</style>
Copy the code
Using the component
The props property to bread crumbs can be passed if there is a jump path. The parameter takes strings or path objects
<MyBread>
<MyBreadItem to="/">The home page</MyBreadItem>
<MyBreadItem>The secondary</MyBreadItem>
<MyBreadItem>Level 3</MyBreadItem>
</MyBread>
Copy the code