Realize breadcrumb function based on vue-router's matched

Realize breadcrumb function based on vue-router's matched

This article mainly introduces the breadcrumb function based on vue-router's matched, and shares it with you. The details are as follows:

As shown in the picture above, this is a common breadcrumb effect. Generally speaking, the content of breadcrumbs has a certain hierarchical relationship. Taking the above picture as an example, first enter the homepage, then click on the left navigation to enter the activity list page under activity management, and then click on a piece of data to enter the activity details page.

This is exactly the same principle as the result obtained by the mached attribute of vue-router, so the breadcrumb effect can be achieved based on this!

Here I used the breadcrumb component and navigation menu component of elementui. Here is the final effect picture:

Routing Configuration

Project Structure:

The side navigation bar is displayed on the home page, electronic digital, clothing and shoes pages, so I created a layout component, pointed the components of these three routes to the component, and wrote the navigation bar and breadcrumbs in the layout component.

Because the implementation of this function depends on the hierarchical nesting relationship of routes, it is necessary to conceive the configuration of routes in advance. My route configuration here is as follows:

const routes = [
 //Match empty route and redirect to root route {
        path:'',
        redirect: '/home',
        meta:{
            showInbreadcrumb:false
        }
    },
    //Root route {
        path:'/home',
        component: ()=>import('@/views/layout/index.vue'),
        name:'home',
        meta:{
            title:"Home",
            showInbreadcrumb:true
        }
    },
    //Electronic digital {
        path:'/electronics',
        name:'Electronic Digital',
        component: ()=>import('@/views/layout/index.vue'),
        redirect: '/electronics/computer',
        meta:{
            title:"Electronic Digital",
            showInbreadcrumb:true
        },
        children:[
            {
                path:'computer',
                name:'computer',
                component()=>import('@/views/electronics/children/computer/index.vue'),
                meta:{
                    title:"Computer",
                    showInbreadcrumb:true
                }
            },
            {
                path:'phone',
                name:'Mobile phone',
                component: ()=>import('@/views/electronics/children/phone/index.vue'),
                meta:{
                    title:"Mobile Phone",
                    showInbreadcrumb:true
                }
            },
            {
                path:'tv',
                name:'TV',
                component: ()=>import('@/views/electronics/children/tv/index.vue'),
                meta:{
                    title:"TV",
                    showInbreadcrumb:true
                }
            }
        ]
    },
    //Clothing, shoes and hats {
        path:'/clothing',
        name:'Clothing, shoes and hats',
        component: ()=>import('@/views/layout/index.vue'),
        redirect: '/clothing/tops',
        meta:{
            title: "Clothing, Shoes and Hats",
            showInbreadcrumb:true
        },
        children:[
            {
                path:'tops',
                name:'Tops',
                component: ()=>import('@/views/clothing/children/tops/index.vue'),
                meta:{
                    title:"Tops",
                    showInbreadcrumb:true
                }
            },
            {
                path:'lower',
                name:'Bottoms',
                component: ()=>import('@/views/clothing/children/lower/index.vue'),
                meta:{
                    title:"Bottoms",
                    showInbreadcrumb:true
                }
            },
            {
                path:'shoes',
                name:'shoes',
                component: ()=>import('@/views/clothing/children/shoes/index.vue'),
                meta:{
                    title:"shoes",
                    showInbreadcrumb:true
                }
            }
        ]
    },
    //Put it at the end. When all the previous routes are not matched, this route will be matched and redirected to the root route {
        path:'*',
        redirect:'/',
        meta:{
            showInbreadcrumb:false
        } 
    },   
]

Here I configured the routes of Home, Electronics, Clothing, Shoes and Hats. These three are first-level routes, among which Electronics, Digital and Clothing, Shoes and Hats have second-level routes. In meta, I customized the data. ShowInbreadcrumb is used to determine whether to display in the breadcrumbs. Title is the name displayed in the breadcrumbs.

Get routing information

Template part:

///src/views/layout/index.vue
<template>
    <div class="layout">
        <!-- Side navigation bar -->
        <div class="sideMenu">
            <el-menu
                default-active="0"
                class="el-menu-vertical-demo"
                >
                    <div v-for="(item,index) in routes" :key="index" :index="index+''">
                        <!-- No secondary menu -->
                        <el-menu-item :index="index+''" v-if="!item.children">
                            <router-link :to="{name:item.name}">{{item.meta.title}}</router-link>
                        </el-menu-item>
                        <!-- With secondary menu -->
                        <el-submenu :index="index+''" v-else>
                            <template slot="title">{{item.meta.title}}</template>
                            <el-menu-item v-for="(item_,index_) in item.children" :key="index_" :index="index+'-'+index_">
                                <router-link :to="{name:item_.name}">{{item_.meta.title}}</router-link>
                            </el-menu-item>
                        </el-submenu>
                    </div>
            </el-menu>
        </div>
        <div class="content">
            <!-- Breadcrumbs -->
            <div class="breadcrumb">
                <el-breadcrumb separator-class="el-icon-arrow-right">
                    <el-breadcrumb-item v-for="(item,index) in breadcrumb" :key="index" :to="{ path: item.path}">{{item.meta.title}}</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <!-- Routing exit -->
            <router-view></router-view>
        </div>
    </div>
</template>

js part:

export default {
    data(){
        return {
        }
    },
    computed:{
        // Side navigation data routes(){
            // Get all routing information from $router.options and filter return this.$router.options.routes.filter((item)=>{
                return item.meta.showInbreadcrumb
            });
        },
        // Breadcrumb data breadcrumb(){
            // Filter according to the showInbreadcrumb field in the route configuration meta let matchedArr = this.$route.matched.filter((item)=>{
                return item.meta.showInbreadcrumb}
            );
            // Because the home page is special, it must always be displayed first in the breadcrumbs. If there is no home page routing information, manually add it to the front if (matchedArr [0].meta.title !== 'Home Page') {
                matchedArr.unshift(
                    {
                        path:'/home',
                        meta:{  
                            title:"Home",
                            showInbreadcrumb:true
                        }
                    },
                )
            }
            return matchedArr;
        },
    }
}

Note: After getting this.$route.matched, you cannot directly append to the result and then filter it, otherwise the page will be messed up and an error will be reported. You should filter first, which will return a new array, and then judge and append the homepage information

Final result

This is the end of this article about implementing the breadcrumbs function based on vue-router matched. For more relevant vue-router matched breadcrumbs content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Several ways to encapsulate breadcrumb function components in Vue3
  • Encapsulation method of Vue breadcrumbs component
  • Vue uses localstorage to implement breadcrumbs
  • Vue element-ui implements dynamic breadcrumb navigation
  • Vue+elementUI dynamically generates breadcrumb navigation tutorial
  • Detailed explanation of dynamic routing with VUE+elementui breadcrumbs
  • Breadcrumb navigation component example code in Vue
  • How to implement Vue dynamic breadcrumbs function
  • Vue2.0 elementUI makes breadcrumb navigation bar

<<:  Solution to the timeout problem when installing docker-compose with PIP

>>:  MySQL 8.0.22 installation and configuration method graphic tutorial

Recommend

Detailed explanation of the buffer pool in MySQL

Everyone knows that data in MySQL needs to be wri...

How does Vue3's dynamic components work?

Table of contents 1. Component Registration 1.1 G...

Vue commonly used high-order functions and comprehensive examples

1. Commonly used high-order functions of arrays S...

Unity connects to MySQL and reads table data implementation code

The table is as follows: Code when Unity reads an...

A simple method to implement scheduled backup of MySQL database in Linux

Here are the detailed steps: 1. Check the disk sp...

CSS specification BEM CSS and OOCSS sample code detailed explanation

Preface During project development, due to differ...

Summary of some small issues about MySQL auto-increment ID

The following questions are all based on the Inno...

Sample code for implementing history in vuex

I have recently been developing a visual operatio...

JS implements dragging the progress bar to change the transparency of elements

What I want to share today is to use native JS to...

Useful codes for web page creation

<br />How can I remove the scroll bar on the...

Two ways to implement square div using CSS

Goal: Create a square whose side length is equal ...

Native js to implement form validation function

Table of contents When developing, analyzing the ...

JavaScript to achieve all or reverse selection function

This article shares the specific code of JavaScri...

How to implement n-grid layout in CSS

Common application scenarios The interfaces of cu...