您也可以使用路径而不是使用查询参数的上述答案来执行此操作。这两个选项都在这里详细讨论:
https://medium.com/ngconf/routing-to-angular-material-dialogs-c3fb7231c177
TL;博士:
创建一个仅在创建时打开模态的虚拟组件:
@Component({
template: ''
})
export class LoginEntryComponent {
constructor(public dialog: MatDialog, private router: Router,
private route: ActivatedRoute) {
this.openDialog();
}
openDialog(): void {
const dialogRef = this.dialog.open(LoginComponent);
dialogRef.afterClosed().subscribe(result => {
this.router.navigate(['../'], { relativeTo: this.route });
});
}
}
然后将虚拟组件添加到您的路线中:
RouterModule.forRoot([
{
path: 'home',
component: BackgroundComponentForModal,
children: [
{
path: 'dialog',
component: LoginEntryComponent
}
]
},
{ path: '**', redirectTo: 'home' }
])