gpt4 book ai didi

javascript - 如何基于当前路由/URL 构建动态面包屑组件?

转载 作者:行者123 更新时间:2023-12-05 02:35:53 32 4
gpt4 key购买 nike

我正在尝试构建一个基于我当前 URL 动态生成链接的 Breadcrumbs 组件


例如

http://localhost:8080/car/create

我的面包屑应该是这样的

Home > car > create


例如

http://localhost:8080/car/ford

我的面包屑应该是这样的

Home > car > ford


例如

http://localhost:8080/car/ford/raptor   

我的面包屑应该是这样的

Home > car > ford > raptor

如何动态构建这样的东西?


我有

<template lang="">
<v-row>
<v-breadcrumbs :items="links" class="black--text">
<template v-slot:divider>
<v-icon>mdi-chevron-right</v-icon>
</template>
</v-breadcrumbs>
</v-row>
</template>
<script>
export default {
name: 'Breadcrumbs',
props: {
title: String
},
data() {
return {
links: [
{
text: 'Home',
disabled: false,
href: '/'
},
{
text: this.title,
disabled: true,
href: 'breadcrumbs_link_2'
}
]
}
}
}
</script>

最佳答案

您需要实现一个computed 属性,如下所示:

const breadcrumb = Vue.component('breadcrumb', {
template: '#breadcrumb',
props: { title: String },
computed: {
links: function() {
return [
{ text: 'Home', disabled: false, href: '/' },
{ text: this.title, disabled: true, href: 'breadcrumbs_link_2' },
...(
(new URL(window.location.href))
.pathname
.split('/')
.slice(1)
.map(seg => ({ text: seg, disabled: false, href: seg }))
)
];
}
}
});

new Vue({ el:"#app", vuetify: new Vuetify(), components: { breadcrumb } });
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script><link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@4.x/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">

<template id="breadcrumb">
<v-row>
<v-breadcrumbs :items="links" class="black--text">
<template v-slot:divider><v-icon>mdi-chevron-right</v-icon></template>
</v-breadcrumbs>
</v-row>
</template>

<v-app id="app">
<breadcrumb title="title_1" />
</v-app>

关于javascript - 如何基于当前路由/URL 构建动态面包屑组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70427567/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com