Encapsulation method of Vue breadcrumbs component

Encapsulation method of Vue breadcrumbs component

Vue encapsulates the breadcrumb component for your reference. The specific contents are as follows

To achieve the effect

Preface

Many e-commerce products need to implement breadcrumb navigation to optimize user experience

1. Primary breadcrumb package components

1. Encapsulate the infrastructure component Bread.vue

<template>
  <div class='xtx-bread'>
    <div class="xtx-bread-item">
      <RouterLink to="/">Home</RouterLink>
    </div>
    <i class="iconfont icon-angle-right"></i>
    <div class="xtx-bread-item">
      <RouterLink to="/category/10000">Secondary navigation</RouterLink>
    </div>
    <i class="iconfont icon-angle-right"></i>
    <div class="xtx-bread-item">
      <span>Third level navigation</span>
    </div>
  </div>
</template>

<style scoped lang='less'>
.xtx-bread{
  display: flex;
  padding: 25px 10px;
  &-item {
    a {
      color: #666;
      transition: all .4s;
      &:hover {
        color: @xtxColor;
      }
    }
  }
  i {
    font-size: 12px;
    margin-left: 5px;
    margin-right: 5px;
    line-height: 22px;
  }
}
</style>

2. Define props to expose parentPath parentName properties, default slots, and dynamically render components

<div class='xtx-bread'>
     <div class="xtx-bread-item">
       <RouterLink to="/">Home</RouterLink>
     </div>
     <i class="iconfont icon-angle-right"></i>
    <template v-if="parentName">
     <div class="xtx-bread-item" v-if="parentName">
       <RouterLink v-if="parentPath" to="/category/10000">{{parentName}}</RouterLink>
       <span v-else>{{parentName}}</span>
     </div>
    </template>
    <i v-if="parentName" class="iconfont icon-angle-right"></i>
    <div class="xtx-bread-item">
      <span> <slot/> </span>
    </div>
  </div>
// Use props to receive data props: {
    parentName: {
      type: String,
      default: ''
    },
    parentPath: {
      type: String,
      default: ''
    }
  }

3. Register components and use verification effects

import Bread from './Bread'
export default {
  install (app) {
    // In Vue2, the parameter is the Vue constructor // In Vue3, the parameter is the instance object of the root component // Configure a global component app.component(Bread.name, Bread)
  }
}

4. Use components

<Bread parentPath="/category/1005000" parentName="Apparel">Flying Knit Series</Bread>
<Bread parentName="Apparel">Flying Weave Series</Bread> //Cannot jump after removing parentPath

2. Advanced packaging Infinitus navigation

Refer to the breadcrumb component of element-ui:

<el-breadcrumb separator="/">
  <el-breadcrumb-item :to="{ path: '/' }">Home</el-breadcrumb-item>
  <el-breadcrumb-item><a href="/" rel="external nofollow" >Activity Management</a></el-breadcrumb-item>
  <el-breadcrumb-item>Activity List</el-breadcrumb-item>
  <el-breadcrumb-item>Event details</el-breadcrumb-item>
</el-breadcrumb>

1. Encapsulate the infrastructure component BrendItem.vue

<template>
  <div class="xtx-bread-item">
    <RouterLink v-if="to" :to="to"><slot /></RouterLink>
    <span v-else><slot /></span>
    <i class="iconfont icon-angle-right"></i>
  </div>
</template>
<script>
export default {
  name: 'XtxBreadItem',
  props: {
    to:
      type: [String, Object] //The value of to can be either a string or an object}
  }
}
</script>
//When using <bread-item to="/xxx/id"></bread-item>
<bread-item :to='{path:"/xxx/id"}'></bread-item>

2. Encapsulate Brend.vue

<template>
  <div class='xtx-bread'>
    <slot />
  </div>
</template>

<script>
export default {
  name: 'XtxBread'
}
</script>

<style scoped lang='less'>
.xtx-bread {
  display: flex;
  padding: 25px 10px;
  :deep(&-item) {
    a {
      color: #666;
      transition: all 0.4s;
      &:hover {
        color: @xtxColor;
      }
    }
  }
  :deep(i) {
    font-size: 12px;
    margin-left: 5px;
    margin-right: 5px;
    line-height: 22px;
  }
}
</style>

3. Registration

import BreadItem from './BreadItem'
import Bread from './Bread'
export default {
  install (app) {
    // In Vue2, the parameter is the Vue constructor // In Vue3, the parameter is the instance object of the root component // Configure a global component app.component(Bread.name, Bread)
    app.component(BreadItem.name, BreadItem)
  }
}

4. Use

// Breadcrumbs
    <BreadItem to="/">Home</XtxBreadItem>
    <BreadItem to="/category/1005000">Clothing</XtxBreadItem>
    <BreadItem >Flying Weave Series</XtxBreadItem>
</XtxBread>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Several ways to encapsulate breadcrumb function components in Vue3
  • Vue encapsulated breadcrumbs component tutorial
  • vue+element-ui table encapsulation tag label using slot
  • Implementation of Vue top tags browsing history
  • Practical vue tags to create cache navigation process
  • Vue basics breadcrumbs and tag detailed explanation

<<:  Linux gzip command compression file implementation principle and code examples

>>:  MySQL performance optimization tips

Recommend

How to use the concat function in mysql

As shown below: //Query the year and month of the...

Summary of the dockerfile-maven-plugin usage guide

Table of contents pom configuration Setting.xml c...

jQuery treeview tree structure application

This article example shares the application code ...

In-depth discussion on auto-increment primary keys in MySQL

Table of contents Features Preservation strategy ...

Detailed explanation of common methods of JavaScript Array

Table of contents Methods that do not change the ...

MySQL 8.0.18 stable version released! Hash Join is here as expected

MySQL 8.0.18 stable version (GA) was officially r...

Bootstrap 3.0 study notes grid system case

Preface In the previous article, we mainly learne...

Mysql5.6.36 script compilation, installation and initialization tutorial

Overview This article is a script for automatical...

Comprehensive understanding of HTML Form elements

As shown below: XML/HTML CodeCopy content to clip...

JavaScript to achieve dynamic table effect

This article shares the specific code for JavaScr...

Talk about important subdirectory issues in Linux system

/etc/fstab Automatically mount partitions/disks, ...

A comparison between the href attribute and onclick event of the a tag

First of all, let's talk about the execution ...