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:
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?
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.
Yes it can be attached to div tag, your route is probably wrong try add / in front of route.
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.
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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With