我有一个像根目录这样的全局常量,我希望每个组件都可以访问。在另一个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);
}
}
答案 0 :(得分:8)
回答你的问题:
使用Constants类的所有组件都需要导入常量文件。
为了使用Constants类,你需要将它注入任何消费组件的构造函数中,从random.component.ts中删除injector.get()函数,如下所示:
export class App {
constructor(constants: Constants) {
this.url = constants.root_dir;
}
}
您也可以将常量类装饰为@Injectable
并将@Inject
装入组件的构造函数中。
在应用程序级别引导共享常量是有益的,这样只能创建一个类的实例并在所有组件之间共享。
答案 1 :(得分:2)
import {Component,bind,provide} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {FORM_DIRECTIVES} from 'angular2/form';
import {Directive, ElementRef, Renderer, Input,ViewChild,AfterViewInit} from 'angular2/core';
import {Constants} from 'src/constants'
import {ViewChild, Component, Injectable} from 'angular2/core';
@Component({
selector: 'my-app',
template: `{{test}}`,
})
export class App {
test: string;
constructor(cs:Constants){
this.test = cs.root_dir;
}
}
bootstrap(App, [Constants]);
<强> Demo 强>
答案 2 :(得分:0)
import {Component} from 'angular2/core'
import { Constants } from './constants'
@Component({
selector: 'test',
template: `
Constants: <strong>{{ urlTemplate }}</strong>
`
providers:[Constants]
})
export class AppComponent{
constructor(constants: Constants){
this.urlTemplate = constants.root_dir;
}
}
您可以在Constants
providers:[Constants]
装饰者@Injectable在这种情况下没有必要,但谷歌建议总是使用你可以在这里看到:https://angular.io/docs/ts/latest/guide/dependency-injection.html#!#why-injectable-
/*
We recommend adding @Injectable() to every service class, even those that don't have dependencies and, therefore, do not technically require it. Here's why:
Future proofing: No need to remember @Injectable when we add a dependency later.
Consistency: All services follow the same rules, and we don't have to wonder why a decorator is missing
*/
//@Injectable()
export class Constants{
root_dir: string;
constructor(){
this.root_dir = 'http://google.com/'
}
}
关于@Inject使用,你可以在这里阅读: what is the difference between using (@Inject(Http) http: Http) or not
现在,如果您想要全局,可以添加bootstrap
//main entry point
import {bootstrap} from 'angular2/platform/browser';
import {AppComponent} from './app';
import { Constants } from './constants';
bootstrap(AppComponent, Constants)
.catch(err => console.error(err));
//import { Injectable } from 'angular2/core'
//@Injectable()
export class Constants{
root_dir: string;
constructor(){
this.root_dir = 'http://google.com/'
}
}
import {Component, Inject} from 'angular2/core'
import { Constants } from './constants';
@Component({
selector: 'test',
template: `
Constants: <strong>{{ urlTemplate }}</strong>
`
})
export class AppComponent{
constructor(@Inject (Constants) constants: Constants){
this.urlTemplate = constants.root_dir;
}
}