Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending routerLink in angular 2?

How can I extend the [routerLink] directive from the Angular Router v3 so I can wrap some custom functionality myself? I've taken a look at the RouterLinkWithHref directive and it appears that is what I want to extend, so I produced this:

ExtendedRouterLinkDirective

import {Directive, Input} from '@angular/core';
import {RouterLinkWithHref} from "@angular/router";
import {Model} from "../Classes/Model.class";

@Directive({
    selector: 'a[extendedRouterLink]'
})
export class ExtendedRouterLinkDirective extends RouterLinkWithHref {
    @Input('extendedRouterLink') model : Model;

    // Future custom logic
}

And I try and use it like so:

<a [extendedRouterLink]="model">

And I include it in the directives array of my component:

...
directives: [ROUTER_DIRECTIVES, ExtendedRouterLinkDirective],
...

Yet, I get the following runtime error:

"Template parse errors: Can't bind to 'extendedRouterLink' since it isn't a known native property"

If I remove the extends RouterLinkWithHref from my directive, it works without errors. What am I doing wrong?

like image 488
marked-down Avatar asked Jul 11 '16 12:07

marked-down


People also ask

Can we use href instead of routerLink?

routerLink is the attribute provided by angular to navigate to different components without reloading the page. Major difference between both is that href kills the state of the current page where routerLink doesnt lose the state of the page.

Can we give routerLink to div?

Yes it can be attached to div tag, your route is probably wrong try add / in front of route.

Can we add routerLink to button?

Using Router links After Angular v4 we can directly add a routerLink attribute on the anchor tag or button. Consider the following template, where routerLink attribute added to the anchor tag. The routerLink directive on the anchor tags give the router control over those elements.

What is routerLink in Angular?

In Angular, RouterLink is a directive for navigating to a different route declaratively. Router. navigate and Router. navigateByURL are two methods available to the Router class to navigate imperatively in your component classes.


1 Answers

I need the same. Using your post, I was able to make it work. Here is what was missing:

@Input() mgiRouterLink: string ngOnInit() 

ngOnInit() {
    this.href = this.routerLink = this.mgiRouterLink  
}

The parent routeLink directive is expecting this.href and this.routerLink to hold the data from the DOM attribute. Now it look like this:

import {Directive, Input} from '@angular/core';
import {RouterLinkWithHref} from "@angular/router";
import {Model} from "../Classes/Model.class";

@Directive({
    selector: 'a[extendedRouterLink]' 
}) 
export class ExtendedRouterLinkDirective extends RouterLinkWithHref {

    @Input() mgiRouterLink: string ngOnInit() 

    ngOnInit() {
        this.href = this.routerLink = this.mgiRouterLink  
    } 
}
like image 80
Richard Lemieux Avatar answered Oct 05 '22 08:10

Richard Lemieux