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

MySQL 5.7.18 zip version installation tutorial

The mysql 5.7.18 zip version of MySQL is not like...

HTML+VUE paging to achieve cool IoT large screen function

Effect demo.html <html> <head> <me...

Detailed explanation of Linux system directories sys, tmp, usr, var!

The growth path from a Linux novice to a Linux ma...

Detailed explanation of MySQL EXPLAIN output columns

1. Introduction The EXPLAIN statement provides in...

Summary of two methods to implement vue printing function

Method 1: Install the plugin via npm 1. Install n...

Solution to 404 Problem of Tomcat Installation in Docker

Find the containerID of tomcat and enter the toma...

A brief discussion on the solution to excessive data in ElementUI el-select

Table of contents 1. Scenario Description 2. Solu...

Mysql dynamically updates the database script example explanation

The specific upgrade script is as follows: Dynami...

CSS warped shadow implementation code

This article introduces the implementation code o...

How to implement the singleton pattern in Javascript

Table of contents Overview Code Implementation Si...

The best way to solve the 1px border on mobile devices (recommended)

When developing for mobile devices, you often enc...

HTML table tag tutorial (19): row tag

The attributes of the <TR> tag are used to ...

How to add a certificate to docker

1. Upgrade process: sudo apt-get update Problems ...

CSS margin overlap and how to prevent it

The vertically adjacent edges of two or more bloc...