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 ConfigurationProject 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:
|
<<: Solution to the timeout problem when installing docker-compose with PIP
>>: MySQL 8.0.22 installation and configuration method graphic tutorial
Table of contents Master-Master Synchronization S...
I believe that the Internet has become an increas...
1. Create a MySQL database 1. Create database syn...
1. Uninstalling MySQL 5.7 1.1查看yum是否安裝過mysql cd y...
When using MySQL to query the database and execut...
Recently, I found that the company's server t...
CocosCreator version 2.3.4 Dragon bone animation ...
Table of contents Missing root location Off-By-Sl...
As the platform continues to grow, the project...
This article example shares the specific code of ...
Preface In databases such as Oracle and SQL Serve...
For Linux system administrators, it is crucial to...
Operating environment: MAC Docker version: Docker...
Code example: public class JDBCDemo3 { public sta...
Table of contents Math Objects Common properties ...