export class myComponent {
public routerLinkVariable = "/home"; // the value of the variable is string!
但是有了变量,你就有机会让它变得动态,对吗?
export class myComponent {
public routerLinkVariable = "/home"; // the value of the variable is string!
updateRouterLinkVariable(){
this.routerLinkVariable = '/about';
}
如果没有方括号,你只能传递字符串,你不能改变它,它是硬编码的,它会像你的整个应用程序。
<a routerLink="/home"></a>
更新:
使用方括号的另一个特性是可以将动态查询参数传递给导航到的链接:
所以添加一个新变量
export class myComponent {
private dynamicQueryParameter = '129';
public routerLinkVariable = "/home";
[routerLink]="link" //when u pass URL value from COMPONENT file
[routerLink]="['link','parameter']" //when you want to pass some parameters along with route
routerLink="link" //when you directly pass some URL
[routerLink]="['link']" //when you directly pass some URL