antd vue的面包屑结合路由的实现记录

使用背景,公司大佬集成的的一个vue为基础的框架,在使用中大佬的按照常规来写的面包屑处理方式,与现实项目中需求有所出入,自己更改了一下,记录一下,防止以后遇到。

Breadcrumb面包屑
  • 场景:场景截图
  • html
<a-breadcrumb :routes="breadcrumb" separator=">">
  <template #itemRender="{route, routes, paths}">
      <span v-if="routes.indexOf(route) === routes.length - 1">
         {{route.breadcrumbName}}
      </span>
      <span v-else @click="breadCrumbClick(route)" :class="route.isStop ? 'cursorClass' : 'normal'">
         {{route.breadcrumbName}}
      </span>
   </template>
</a-breadcrumb>
  • computed
computed: {
      breadcrumb() {
         let arr = [];
         this.$route.matched.map(route => {
            let parent = route.meta.parent;
            if(parent) {
               arr.push({
                  breadcrumbName: i18nRender(parent.title),
                  path: parent.path,
                  isStop:true
               });
            }
            arr.push({
               breadcrumbName: i18nRender(route.meta.title),
               path: route.path,
               isStop:false
            });
         });
         arr.splice(0, 1);
         return arr;
      }
   },
  • methods
methods:{
	breadCrumbClick(route) {
      if(route.isStop) {
         this.$router.push({
            path:route.path
         });
      }
	},
}
  • i18Render方法
export function i18nRender (key) {
   return i18n.t(`${key}`);
}

export default i18n;

大佬使用了 vue-i18n插件
链接: 网址.
此插件是是 Vue.js 的国际化插件。它可以轻松地将一些本地化功能集成到你的 Vue.js 应用程序中。

  • vue-i18 使用方式
npm :
npm install vue-i18n
yarn 
yarn add vue-i18n

如果在一个模块系统中使用它,你必须通过 Vue.use() 明确地安装 vue-i18n:

import Vue from 'vue'
import VueI18n from 'vue-i18n'

Vue.use(VueI18n)

如果使用全局的 script 标签,则无须如此 (手动安装)

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue 3 中,Ant Design Vue 的 Tabs 组件的使用方法与 Vue 2 中基本相同。具体实现步骤如下: 1. 在路由配置中,添加 `meta` 字段用于标识当前路由是否需要在标签栏中显示,例如: ```javascript const routes = [ { path: '/', name: 'Home', component: Home, meta: { title: '首页', keepAlive: true, // 是否缓存组件 showTab: true, // 是否在标签栏中显示 }, }, // ... ]; ``` 2. 在 App.vue 中,使用 Tabs 组件来渲染标签栏,并使用 `router-view` 组件来渲染当前路由对应的组件: ```vue <template> <div> <a-tabs v-model:selectedTabKey="selectedTabKey" type="editable-card" hide-add @edit="handleTabEdit" style="margin: 0 24px;"> <a-tab-pane v-for="(tab, index) in tabs" :key="tab.key" :tab="tab.title" :closable="index !== 0" @close="handleTabClose(index)"> </a-tab-pane> </a-tabs> <router-view /> </div> </template> <script> import { ref } from 'vue'; export default { setup() { const selectedTabKey = ref('/'); const tabs = ref([ { key: '/', title: '首页', }, ]); const addTab = (key, title) => { const index = tabs.value.findIndex((tab) => tab.key === key); if (index === -1) { tabs.value.push({ key, title, }); } }; const removeTab = (key) => { const index = tabs.value.findIndex((tab) => tab.key === key); if (index !== -1) { tabs.value.splice(index, 1); } }; const handleTabClose = (index) => { const { key } = tabs.value[index]; removeTab(key); selectedTabKey.value = tabs.value[tabs.value.length - 1].key; }; const handleTabEdit = (targetKey, action) => { if (action === 'add') { selectedTabKey.value = targetKey; addTab(targetKey, ''); } else if (action === 'remove') { handleTabClose(tabs.value.findIndex((tab) => tab.key === targetKey)); } }; return { selectedTabKey, tabs, handleTabClose, handleTabEdit, }; }, }; </script> ``` 在这个示例中,我们使用了 `selectedTabKey` 和 `tabs` 两个响应式变量来分别存储当前选中的标签页和所有已打开的标签页。在 `addTab` 方法中,我们使用 `tabs` 数组来存储已打开的标签页,防止重复添加。在 `removeTab` 方法中,我们使用 `tabs` 数组来删除已关闭的标签页。在 `handleTabClose` 方法中,我们使用 `tabs` 数组来关闭标签页,并将选中的标签页设置为最后一个标签页。在 `handleTabEdit` 方法中,我们通过判断用户的操作来决定是添加标签页还是关闭标签页。在 Vue 3 中,我们使用 `ref` 来创建响应式变量。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值