Difference between [routerLink] and routerLink

前端 未结 3 1520
长发绾君心
长发绾君心 2020-12-07 13:49

What is the difference between [routerLink] and routerLink ? How should you use each one?

3条回答
  •  庸人自扰
    2020-12-07 14:27

    You'll see this in all the directives:

    When you use brackets, it means you're passing a bindable property (a variable).

      
    

    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.

    
    

    UPDATE :

    The other speciality about using brackets specifically for routerLink is that you can pass dynamic parameters to the link you're navigating to:

    So adding a new variable

    export class myComponent {
            private dynamicParameter = '129';
            public routerLinkVariable = "/home"; 
    

    Updating the [routerLink]

      
    

    When you want to click on this link, it would become:

      
    

提交回复
热议问题