在一个单页面或者一个弹窗里面,不用路由来实现element ui的面包屑的跳转等功能 对element ui 的面包屑breadcrumb进行二次封装,写成一个组件,你可以直接复制粘贴,就是一个完整的组件。
引入组件:
//父组件中引入
import Mybreadcrumb from "../mybreadcrumb.vue";
父组件中引入写上
<Mybreadcrumb
v-model="innerPath"
:label-args="{ title2: active.title2, title3: active.title3 }"
:showInRoot="showInRoot"
:path="{
label: '财务报销费用',
key: '1',
children: [
{
label: '{title2}',//动态标题
key: '2',
children: [
{
label: '{title3}', //动态标题
key: '3',
},
],
},
],
}"
></Mybreadcrumb>
//这里需要引入需要跳转的子组件
<div style="height: calc(100% - 45px)">
//一级页面
<Indexpage
v-if="innerPath == 1"
@sentIndexFn="sentIndexFn"
style="height: 100%"
></Indexpage>
//二级页面
<Detail
v-else-if="innerPath == 2"
:detailData="detailData"
style="height: 100%"
></Detail>
//三级.....
</div>
**参数配置**
data(){
return{
showInRoot: true,
innerPath: 1,
active: { title2: "", title3: "" },
}
下面是组件
这里是组件html
<template >
<div class="inner-breadcrumb" v-show="showInRoot || value!=root" style="height:40px;padding:0 15px;">
<el-breadcrumb class="breadcrumb" separator=">" style="float:left;">
<el-breadcrumb-item v-for="(node, index) in pathArr" :key="index" style="line-height:40px">
<span v-if="node.disable && index<pathArr.length-1" class="disable" :class="{'last':index==pathArr.length-1}">{{parse(node.label)}}</span>
<a v-else :class="{'last':index==pathArr.length-1}" href="javascript:;" @click="change(node.key,node.disable)">{{parse(node.label)}}</a>
</el-breadcrumb-item>
</el-breadcrumb>
<el-button v-if="showBack&&pathArr.length>1" style="float:right;padding:0;" type="text" @click="back">返回上一级</el-button>
</div>
</template>
下面时组件参数及配置:
export default {
name: "InnerBreadcrumb",
props: {
value: {
type: String,
},
path: {
type: Object,
required: true,
default: {
}
},
showBack:{
type: Boolean,
default: false
},
showInRoot: {
type: Boolean,
default: false
},
labelArgs:{
type: Object,
default: {}
}
},
data() {
return {
pathMap: null,
root: null,
pathArr: []
};
},
watch: {
value(val) {
this.buildPath(val);
}
},
methods: {
init() {
let path = this.path;
this.root = path.key;
let pathMap = {};
let traverse = (node, parentKey) => {
node.parentKey = parentKey;
pathMap[node.key] = node;
if (node.children && node.children.length) {
for (let i = 0; i < node.children.length; i++) {
const child = node.children[i];
traverse(child, node.key);
}
}
};
traverse(path);
this.pathMap = pathMap;
},
buildPath(key) {
if(!key)
return;
let node = this.pathMap[key];
if (!node) {
return;
}
let arr = [];
while (node) {
arr.push(node);
node = this.pathMap[node.parentKey];
}
arr.reverse();
this.pathArr = arr;
},
parse(label){
const reg = /\{[^\}]+\}/g;
let labelCopy = label;
let match;
while((match = reg.exec(label))!=null){
let key = match[0].substr(1,match[0].length-2);
let value = this.labelArgs[key];
if (value){
labelCopy = labelCopy.replace(match[0],value);
}
}
return labelCopy;
},
change(key, disable) {
if (disable) return;
this.$emit("input", key);
},
back(){
if(this.pathArr && this.pathArr.length>1){
for (let i = this.pathArr.length-2; i >=0; i--) {
const node = this.pathArr[i];
if (!node.disable){
this.change(node.key);
return;
}
}
}
console.warn('InnerBreadcrumb 没有非disalbe的上级路径');
}
},
created() {},
mounted() {
this.init();
this.buildPath(this.value);
}
};
这里是组件样式,可更改
<style lang="scss">
:root .inner-breadcrumb {
// background-color:#e8e8e8 ;
.breadcrumb{
span {
font-weight: normal;
color: #0D867F!important;
}
a {
font-weight: normal;
color: #0D867F!important;
color: #0D867F!important;
}
a:hover{
color: #3d9e99!important;
}
.last{
font-weight: bold!important;
cursor: default!important;
color: #0D867F!important;
}
.disable{
cursor: default!important;
color: #0D867F!important;
}
}
}
</style>
|