vue面包屑組件的封裝方法
vue中自己封裝面包屑組件,供大家參考,具體內(nèi)容如下
要實(shí)現(xiàn)效果
前言
很多電商類產(chǎn)品都需要實(shí)現(xiàn)面包屑導(dǎo)航,用來(lái)優(yōu)化用戶體驗(yàn)
一、初級(jí)面包屑封裝組件
1.封裝基礎(chǔ)結(jié)構(gòu)組件 Bread.vue
<template> <div class='xtx-bread'> <div class="xtx-bread-item"> <RouterLink to="/">首頁(yè)</RouterLink> </div> <i class="iconfont icon-angle-right"></i> <div class="xtx-bread-item"> <RouterLink to="/category/10000">二級(jí)級(jí)導(dǎo)航</RouterLink> </div> <i class="iconfont icon-angle-right"></i> <div class="xtx-bread-item"> <span>三級(jí)導(dǎo)航</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.定義 props 暴露 parentPath parentName 屬性,默認(rèn)插槽,動(dòng)態(tài)渲染組件
<div class='xtx-bread'> <div class="xtx-bread-item"> <RouterLink to="/">首頁(yè)</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> //用props接收數(shù)據(jù) props: { parentName: { type: String, default: '' }, parentPath: { type: String, default: '' } }
3.注冊(cè)組件,使用驗(yàn)證效果
import Bread from './Bread' export default { install (app) { // Vue2中,參數(shù)是Vue構(gòu)造函數(shù) // Vue3中,參數(shù)是根組件的實(shí)例對(duì)象 // 配置一個(gè)全局組件 app.component(Bread.name, Bread) } }
4.使用組件
<Bread parentPath="/category/1005000" parentName="服飾">飛織系列</Bread> <Bread parentName="服飾">飛織系列</Bread> //parentPath去掉后不能實(shí)現(xiàn)跳轉(zhuǎn)
二、高級(jí)封裝 無(wú)限極導(dǎo)航
參考element-ui的面包屑組件:
<el-breadcrumb separator="/"> <el-breadcrumb-item :to="{ path: '/' }">首頁(yè)</el-breadcrumb-item> <el-breadcrumb-item><a href="/" rel="external nofollow" >活動(dòng)管理</a></el-breadcrumb-item> <el-breadcrumb-item>活動(dòng)列表</el-breadcrumb-item> <el-breadcrumb-item>活動(dòng)詳情</el-breadcrumb-item> </el-breadcrumb>
1.封裝基礎(chǔ)結(jié)構(gòu)組件 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] //to的值即可以是字符串,也可以是對(duì)象 } } } </script> //使用時(shí) <bread-item to="/xxx/id"></bread-item> <bread-item :to='{path:"/xxx/id"}'></bread-item>
2.封裝 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.注冊(cè)
import BreadItem from './BreadItem' import Bread from './Bread' export default { install (app) { // Vue2中,參數(shù)是Vue構(gòu)造函數(shù) // Vue3中,參數(shù)是根組件的實(shí)例對(duì)象 // 配置一個(gè)全局組件 app.component(Bread.name,Bread) app.component(BreadItem.name, BreadItem) } }
4.使用
// 面包屑 <Bread> <BreadItem to="/">首頁(yè)</XtxBreadItem> <BreadItem to="/category/1005000">服飾</XtxBreadItem> <BreadItem >飛織系列</XtxBreadItem> </XtxBread>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue在index.html中引入靜態(tài)文件不生效問(wèn)題及解決方法
這篇文章主要介紹了vue在index.html中引入靜態(tài)文件不生效問(wèn)題及解決方法,本文給大家分享兩種原因分析,通過(guò)實(shí)例代碼講解的非常詳細(xì) ,需要的朋友可以參考下2019-04-04vue3-print-nb實(shí)現(xiàn)頁(yè)面打印(含分頁(yè)打印)示例代碼
大多數(shù)后臺(tái)系統(tǒng)中都存在打印的需求,在有打印需求時(shí),對(duì)前端來(lái)說(shuō)當(dāng)然是直接打印頁(yè)面更容易,下面這篇文章主要給大家介紹了關(guān)于vue3-print-nb實(shí)現(xiàn)頁(yè)面打印(含分頁(yè)打印)的相關(guān)資料,需要的朋友可以參考下2024-01-01使用Vue3和p5.js實(shí)現(xiàn)交互式圖像動(dòng)畫(huà)
這篇文章主要介紹了如何用Vue3和p5.js打造一個(gè)交互式圖像動(dòng)畫(huà),文中給出了詳細(xì)的代碼示例,本代碼適用于需要在網(wǎng)頁(yè)中實(shí)現(xiàn)圖像滑動(dòng)效果的場(chǎng)景,例如圖片瀏覽、相冊(cè)展示等,感興趣的小伙伴跟著小編一起來(lái)看看吧2024-06-06在?Vue?項(xiàng)目中如何引用?assets?文件夾中的圖片(方式匯總)
Vue中引用assets文件夾中的圖片有多種方式,在.vue文件的模板部分,使用相對(duì)路徑或通過(guò)@別名引用圖片,在CSS中,通過(guò)相對(duì)路徑或@別名引用圖片作為背景,在JavaScript中,通過(guò)import語(yǔ)句導(dǎo)入圖片資源,并使用v-bind在模板中綁定顯示,這些方法均可有效管理和引用項(xiàng)目中的圖片資源2024-09-09Vue + element實(shí)現(xiàn)動(dòng)態(tài)顯示后臺(tái)數(shù)據(jù)到options的操作方法
最近遇到一個(gè)需求需要實(shí)現(xiàn)selector選擇器中選項(xiàng)值options 數(shù)據(jù)的動(dòng)態(tài)顯示,而非寫(xiě)死的數(shù)據(jù),本文通過(guò)實(shí)例代碼給大家分享實(shí)現(xiàn)方法,感興趣的朋友一起看看吧2021-07-07