我曾经做过一个自动面包屑生成器,它只依赖于你的路由中的静态数据。我设法找到了它,所以你去吧,以防它有帮助:
export class RouteBreadcrumbsComponent {
breadcrumbs = [];
constructor(
private router: Router,
) {
let buffer = [];
/*
Listen for router events in reverse order (to get all events).
From the snapshots, return its path and its breadcrumb.
*/
this.router.events.pipe(
filter(event => event instanceof ActivationEnd),
map((event: ActivationEnd) => [
event.snapshot.data && event.snapshot.data.crumb || undefined,
this.determineCorrectPath(event.snapshot),
]),
filter(([crumb, url]) => !!crumb),
).subscribe(([crumb, url]) => buffer.push({ crumb, url }));
this.router.events.pipe(
filter(event => event instanceof NavigationEnd)
).subscribe(event => {
this.breadcrumbs = buffer.reverse();
buffer = [];
});
}
determineCorrectPath(snapshot: ActivatedRouteSnapshot) {
const path = snapshot.routeConfig.path;
const params = snapshot.params;
// Path = route with a param, so get the param
if (path.startsWith(':'))
return params[path.replace(':', '')];
// Path = '' = route group or root route of a lazy loaded module, so take the parent path
else if (!path)
return snapshot.parent.routeConfig.path;
// Path can be used as is
else
return path;
}
buildRoutingArray(crumbIndex: number) {
return this.breadcrumbs
.slice(0, crumbIndex + 1)
.map(crumb => crumb.url)
.join('/');
}
}
您所要做的就是为data: { crumb: 'Your component crumb' }
您的每条路线提供一个,它应该可以工作。