-1

I'm working on an Angular4 project. Everything works great till now, but I'm finding myself hard coding the path of routerlink directly into the html templates:

<a routerLink="/example" >link</a>

I want to avoid it somehow and bind it to the routing module I have.

Is it possible (and logical)? What is the best practice regarding paths?

Thanks

maryum375
  • 727
  • 1
  • 10
  • 22

1 Answers1

1

For binding you need [] or {{}} (never both together) like

[routerLink]="fieldWithPath"

or

routerLink="{{fieldWithPath}}"
export class MyComponent {
  fieldWithPath:string = '/example';
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • I'm trying to figure out how to get the link directly from the router and not write '/example' on every component that should link to /example – maryum375 Dec 13 '17 at 12:01
  • Not sure what you mean with "directly from the router". You can create a my-example-link component if you need it often or add a getter to you component that returns the value from a constant or similar. – Günter Zöchbauer Dec 13 '17 at 12:07
  • I have a Routing module that define all the routes in the app. it has a line {path: 'example', component: exampleComponent} I want to import the path from there instead of decoupling it in my code – maryum375 Dec 13 '17 at 12:20
  • 1
    Create constants and use them in routes and in components. – Günter Zöchbauer Dec 13 '17 at 12:24