Поскольку речь идет об угле, вы можете просто сделать это вместо:
<button (click)="routeToOtherPage()">Link</button>
с помощью
routeToOtherPage() {
this.router.navigate(["/other-page"]);
}
Вы также можете написать свою собственную директиву, чтобы встроить это, что-то вродеэто:
@Directive({
selector: "[clickRouterLink]"
})
export class ClickRouterLinkDirective {
@Input()
private clickRouterLink: any | any[];
@HostListener("click")
public handleLinkClicked() {
// Crude check for whether an array has been provided.
// You might want to make this better (or even compare with the implementation of routerLink).
const route = this.clickRouterLink && typeof this.clickRouterLink.length === "number"
? this.clickRouterLink
: [this.clickRouterLink];
this.router.navigate(route);
}
constructor(private router: Router) {}
}
А потом
<button clickRouterLink="events">Link</button>
<button [clickRouterLink]="['events', event.id]">Link</button>