From ca1831d78735b288ec172590cb9e71f6a35c66b6 Mon Sep 17 00:00:00 2001 From: Ivan Demidov Date: Mon, 12 Oct 2020 16:31:27 +0300 Subject: [PATCH] fix: incorrect resolve multi parents, close #105 --- src/index.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 583be6b..9489418 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,9 +17,22 @@ class VueBreadcrumbs implements PluginObject> { options.render = undefined; } + Object.defineProperties(Vue.prototype, { $breadcrumbs: { get(): RouteRecord[] { + function findParents(this: Vue, routeName: string, matches: RouteRecord[] = []): RouteRecord[] { + const [routeParent]: RouteRecord[] = this.$router.resolve({ name: routeName }).route.matched; + matches.unshift(routeParent); + const parentName: string = routeParent.meta?.breadcrumb?.parent; + + if (parentName) { + return findParents.call(this, routeParent.meta.breadcrumb.parent, matches); + } + + return matches; + } + return this.$route.matched .flatMap((route: RouteRecord) => { let routeRecord: RouteRecord[] = [route]; @@ -30,8 +43,7 @@ class VueBreadcrumbs implements PluginObject> { } if (breadcrumb?.parent) { - const matched = this.$router.resolve({ name: breadcrumb.parent }).route.matched - + const matched = findParents.call(this, breadcrumb.parent, []); routeRecord = [...matched, ...routeRecord]; }