我认为不可能通过state
通过一个按钮。如果我们检查源代码routerLink
, 我们可以看到...
when not an a
tag:
@Directive({selector: ':not(a):not(area)[routerLink]'})
state
不包含在extras
:
@HostListener('click')
onClick(): boolean {
const extras = {
skipLocationChange: attrBoolValue(this.skipLocationChange),
replaceUrl: attrBoolValue(this.replaceUrl),
};
this.router.navigateByUrl(this.urlTree, extras);
return true;
}
source https://github.com/angular/angular/blob/8.2.8/packages/router/src/directives/router_link.ts#L163
而当我们有一个a
tag:
@Directive({selector: 'a[routerLink],area[routerLink]'})
它包括:
@HostListener('click', [/** .... **/])
onClick(/** .... **/): boolean {
// .....
const extras = {
skipLocationChange: attrBoolValue(this.skipLocationChange),
replaceUrl: attrBoolValue(this.replaceUrl),
state: this.state // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< here!
};
this.router.navigateByUrl(this.urlTree, extras);
return false;
}
source https://github.com/angular/angular/blob/8.2.8/packages/router/src/directives/router_link.ts#L263
因此,您的选择是将该链接设置为看起来像按钮,或者然后在单击按钮时调用执行导航的函数,如其他答案中所示,在这里我请参考由阿博法兹R:
this.router.navigate(['home'], this.navExtra);