【发布时间】:2021-07-27 11:20:33
【问题描述】:
我正在 Angular 12 中构建一个基本的笔记应用程序,其中笔记存储在 Firebase 中。注释包含文本,但也可以作为更多注释的父节点,例如:
Note
Note
|- Note
|- Note
|- Note
|- Note
|- Note
Note
您可以从/notes/{{note.id}} 查看笔记的子笔记列表,并在/edit/{{note.id}} 编辑笔记本身。如果/notes/ 的 id 为 null,它会抓取所有顶级注释。
问题:一切正常,直到我尝试查看层次结构第二级的笔记列表。 routerLink 似乎将/edit/{{note.id}} 和/notes/{{note.id}} 添加到导航堆栈,例如:
Note1
|- Note2 <-- click '>' button on note2 in list view
|- Note3 <-- expect to see note3 in list view
我没有查看notes/2,而是转到edit/2。在浏览器中单击返回将我带到我想要的 notes/2 的正确路线,允许我在列表视图中看到 note3。为什么单击图标按钮时会在路由器中添加/edit/{{note.id}}?
编辑:在这里演示问题的 StackBlitz 链接: https://stackblitz.com/edit/angular-hcmvab
相关代码sn-ps如下:
app-routing.module.ts
const routes: Routes = [
{ path: '', redirectTo: 'notes', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'notes',
children: [
{ path: '', component: NotesComponent },
{ path: ':id', component: NotesComponent },
],
},
{ path: 'edit/:id', component: NoteDetailsComponent }
];
notes.component.ts
export class NotesComponent implements OnInit {
parentID = this.route.snapshot.paramMap.get('id');
notes: any;
constructor(
private route: ActivatedRoute,
private notesService: NotesService
) {}
ngOnInit(): void {
this.retrieveNotes();
}
retrieveNotes(): void {
this.notesService
.getChildNotes(this.parentID)
.snapshotChanges()
.pipe(
map((changes: any) =>
changes.map((c: any) => ({
id: c.payload.doc.id,
...c.payload.doc.data(),
}))
)
)
.subscribe((data) => {
this.notes = data;
});
}
}
notes.component.html
<mat-list>
<mat-list-item *ngFor="let note of notes" [routerLink]="['/edit', note.id]">
<mat-icon fontSet="material-icons-outlined" mat-list-icon>description_outlined</mat-icon>
<div mat-line>{{note?.title}}</div>
<div class="spacer"></div>
<button mat-icon-button *ngIf="note.children.length > 0" [routerLink]="['/notes', note.id]">
<mat-icon>arrow_forward_ios</mat-icon>
</button>
</mat-list-item>
</mat-list>
【问题讨论】: