Skip to content

Commit

Permalink
perf: dynamic breadcrumbs, issue #99, #100
Browse files Browse the repository at this point in the history
  • Loading branch information
Scrum committed Jul 21, 2020
1 parent 81997c5 commit cc58782
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Dictionary<T> = { [key: string]: T }
type CallbackFunction = (params: Dictionary<string>) => string;

type Breadcrumbs = {
label: string | CallbackFunction
label: string
parent: string
}

Expand All @@ -23,9 +23,14 @@ class VueBreadcrumbs implements PluginObject<ComponentOptions<Vue>> {
return this.$route.matched
.flatMap((route: RouteRecord) => {
let routeRecord: RouteRecord[] = [route];
let breadcrumb = route.meta?.breadcrumb;

if (route.meta?.breadcrumb?.parent) {
const matched = this.$router.resolve({ name: route.meta.breadcrumb.parent }).route.matched
if (typeof breadcrumb === 'function') {
breadcrumb = breadcrumb.call(this, this.$route.params);
}

if (breadcrumb?.parent) {
const matched = this.$router.resolve({ name: breadcrumb.parent }).route.matched

routeRecord = [...matched, ...routeRecord];
}
Expand All @@ -45,12 +50,12 @@ class VueBreadcrumbs implements PluginObject<ComponentOptions<Vue>> {
getBreadcrumb(bc: string | CallbackFunction | Breadcrumbs): string {
let name = bc;

if (typeof name === 'object') {
name = name.label
if (typeof name === 'function') {
name = name.call(this, this.$route.params);
}

if (typeof name === 'function') {
return name.call(this, this.$route.params);
if (typeof name === 'object') {
name = name.label
}

return name;
Expand Down

0 comments on commit cc58782

Please sign in to comment.