Imagine having a collection of links that you want to organize in a separate file like this:
export const SITE_LINKS = [
{ text: 'About', path: '/about' },
{ text: 'Projects', path: '/projects}
]
You plan to utilize these links in 2 different places: header.component.ts
and footer.component.ts
<ul>
<li *ngFor="let link of links">
<a [routeLink]="link.path">{{ link.text }}</a>
</li>
</ul>
What is the recommended approach for adding constants in Angular?
There are two main methods that can be used:
- Include them in the
environment.ts
files
export const environment = {
production: false,
siteLinks: SITE_LINKS
};
- Add them as dependencies so the DI framework can resolve them.
const siteLinks = new InjectionToken<string[]>('site links')
providers: [
{ provide: siteLinks: useValue: SITE_LINKS }
]
I'm interested in knowing which approach is more preferable and in what scenarios one should be chosen over the other?