作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在基于 Angular 7 的应用程序中实现面包屑。
下面提到了包含面包屑组件的根组件的 HTML 模板(面包屑在路由器导出之外)
<app-layout>
<div>
<app-breadcrumb></app-breadcrumb>
<router-outlet></router-outlet>
</div>
</app-layout>
ngOnInit() {
this.router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe((router) => {
let snapshot = this.router.routerState.snapshot;
this.breadcrumbs = [];
let url = snapshot.url;
let routeData: Data = snapshot.root.data;
let label = routeData['breadcrumb'];
let params = snapshot.root.params;
this.breadcrumbs.push({
url: url,
label: label,
params: params
});
});
<nav aria-label='breadcrumb'>
<ol class='breadcrumb'>
<li *ngFor='let breadcrumb of breadcrumbs'>
<a [routerLink]='[breadcrumb.url, breadcrumb.params]' routerLinkActive='active'>{{ breadcrumb.label }}</a>
</li>
</ol>
</nav>
const routes: Routes = [
{ path: 'folders', component: FolderManagementComponent, data: { breadcrumb: 'Home' } },
{ path: 'folders/list-documents', component: ListDocumentsComponent, data: { breadcrumb: 'Documents' } },
{ path: '', redirectTo: '/folders', pathMatch: 'full', data: { breadcrumb: 'Home' }}
];
最佳答案
使用以下代码更新 Breadcrumb 组件 ts 文件中的路由器订阅逻辑。
ngOnInit() {
this.router.events
.pipe(filter(event => event instanceof NavigationEnd))
.pipe(map(() => this.activatedRoute))
.pipe(map((route) => {
while (route.firstChild) { route = route.firstChild; }
return route;
}))
.pipe(filter(route => route.outlet === PRIMARY_OUTLET))
.subscribe(route => {
let snapshot = this.router.routerState.snapshot;
this.breadcrumbs = [];
let url = snapshot.url;
let routeData = route.snapshot.data;
console.log(routeData);
let label = routeData['breadcrumb'];
let params = snapshot.root.params;
this.breadcrumbs.push({
url: url,
label: label,
params: params
});
});
关于angular - 如何在 Angular 7 中实现面包屑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53552615/
我是一名优秀的程序员,十分优秀!