问题描述
[routerLink]
和 routerLink
有什么区别?
推荐答案
您会在所有指令中看到这一点:
You'll see this in all the directives:
使用方括号表示您正在传递可绑定的属性(变量)。
When you use brackets, it means you're passing a bindable property (a variable).
<a [routerLink]="routerLinkVariable"></a>
因此可以在您的类中定义此变量(routerLinkVariable),并且其值应如下所示:
So this variable (routerLinkVariable) could be defined inside your class and it should have a value like below:
export class myComponent {
public routerLinkVariable = "/home"; // the value of the variable is string!
但是有了变量,您就有机会使其变得动态吗?
But with variables, you have the opportunity to make it dynamic right?
export class myComponent {
public routerLinkVariable = "/home"; // the value of the variable is string!
updateRouterLinkVariable(){
this.routerLinkVariable = '/about';
}
在没有括号的情况下,您仅传递字符串,并且不能更改
Where as without brackets you're passing string only and you can't change it, it's hard coded and it'll be like that throughout your app.
<a routerLink="/home"></a>
UPDATE:
关于专门用于routerLink的括号的另一项特色是,您可以将动态参数传递到要导航到的链接:
The other speciality about using brackets specifically for routerLink is that you can pass dynamic parameters to the link you're navigating to:
因此添加一个新变量
export class myComponent {
private dynamicParameter = '129';
public routerLinkVariable = "/home";
更新[routerLink]
Updating the [routerLink]
<a [routerLink]="[routerLinkVariable,dynamicParameter]"></a>
当您想单击此链接时,它将变为:
When you want to click on this link, it would become:
<a href="/home/129"></a>
这篇关于[routerLink]和routerLink之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!