gpt4 book ai didi

angular 5 在某些组件中有面包屑

转载 作者:太空狗 更新时间:2023-10-29 19:26:17 35 4
gpt4 key购买 nike

在我的 Angular 5 项目中,我正在尝试为我的组件创建面包屑,我遵循了本指南 https://medium.com/@bo.vandersteene/angular-5-breadcrumb-c225fd9df5cf

通过 thin breadcrum 只显示 app-component.html 文件,它不会显示在其他组件中......我希望在某些组件中有 beadcrumb。

喜欢:

export class BreadcrumbComponent implements OnInit {
breadcrumbs$;
// Build your breadcrumb starting with the root route of your current activated route
constructor(private activatedRoute: ActivatedRoute,
private router: Router) {
this.breadcrumbs$ = this.router.events
.filter(event => event instanceof NavigationEnd)
.distinctUntilChanged()
.map(event => this.buildBreadCrumb(this.activatedRoute.root));

}

ngOnInit() {
}

buildBreadCrumb(route: ActivatedRoute, url: string = '',
breadcrumbs: Array<BreadCrumb> = []): Array<BreadCrumb> {
// If no routeConfig is avalailable we are on the root path
const label = route.routeConfig ? route.routeConfig.data[ 'breadcrumb' ] : 'Home';
const path = route.routeConfig ? route.routeConfig.path : '';
// In the routeConfig the complete path is not available,
// so we rebuild it each time
const nextUrl = `${url}${path}/`;
const breadcrumb = {
label: label,
url: nextUrl
};
const newBreadcrumbs = [ ...breadcrumbs, breadcrumb ];
if (route.firstChild) {
// If we are not on our current path yet,
// there will be more children to look after, to build our breadcumb
return this.buildBreadCrumb(route.firstChild, nextUrl, newBreadcrumbs);
}
return newBreadcrumbs;
}
}

html:

<ol class="breadcrumb">
<li *ngFor="let breadcrumb of breadcrumbs$ | async"
class="breadcrumb-item">
<a [routerLink]="[breadcrumb.url, breadcrumb.params]">
{{ breadcrumb.label }}
</a>
</li>
</ol>

我将路由器事件移至构造函数以检测路由器更改,但它在其他组件中仍未显示任何内容.. 在他的代码中,如果我们转到内容组件,它也不会显示任何内容。

我该如何解决这个问题?

最佳答案

此组件只会在 app.component 中工作,因为它正在监听路由器事件。

app.component's 中创建的任何组件<router-outlet>导航完成后创建。因此,没有您的子组件可以响应的导航事件。

我建议您创建 breadcrumbs$app.component 中流并重构您的面包屑组件以接受 ActivatedRoute作为@Input() .

关于angular 5 在某些组件中有面包屑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49830151/

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