У меня есть глобальные константы, такие как корневой каталог, к которому я хочу, чтобы каждый компонент имел доступ. В другом вопросе stackoverflow ответ заключался в том, чтобы создать класс констант и импортировать его для каждого компонента.
Есть ли способ загрузить класс констант, чтобы каждый компонент приложения имел к нему доступ без какого-либо дополнительного импорта?
У меня это до сих пор, но он не работает, как я могу повысить класс констант, а затем получить доступ к моим компонентам?
constants.ts
export class Constants{
root_dir: string;
constructor(){
this.root_dir = 'http://google.com/'
}
}
main.ts
import {bootstrap} from 'angular2/platform/browser'
import {Constants} from './constants'
bootstrap([
provide(Constants, {useClass: Constants})
]);
random.component.ts
import {Component, bind} from 'angular2/core';
import {Injector} from 'angular2/core';
@Component({
selector: 'my-app',
template: `{{test}}`
})
export class RandomComponent{
test: string;
constructor(){
this.test = injector.get(Constants.root_dir);
}
}