Typescript Angular2应用范围变量
是否有办法设置所有组件都可以使用的特定应用范围变量/常量?如果是,在何处声明,以及如何在组件中引用 我能想到的是Typescript Angular2应用范围变量,typescript,angular,Typescript,Angular,是否有办法设置所有组件都可以使用的特定应用范围变量/常量?如果是,在何处声明,以及如何在组件中引用 我能想到的是 export var SharedValues = { Title: "aaaaa", Something: "bbbbb" } 然后在组件中导入并使用它 我可以在main.ts中声明一些东西,然后直接引用它或类似的东西吗?您可以将其打包到一个类中,并将其用作服务。比如说, @Injectable() export class SharedValues {
export var SharedValues = {
Title: "aaaaa",
Something: "bbbbb"
}
然后在组件中导入并使用它
我可以在main.ts中声明一些东西,然后直接引用它或类似的东西吗?您可以将其打包到一个类中,并将其用作服务。比如说,
@Injectable()
export class SharedValues {
public Title = 'aaaaa';
public Something = 'bbbbb';
}
然后,如果您正在使用RC5,请在模块中包含
import { SharedValues } from 'some/path/shared-values.service';
@NgModule({
// declarations, imports, bootstrap...
providers: [
// your other providers
SharedValues,
]
}) export class AppModule {}
如果您使用的是RC4或以前版本,请添加到引导
import { SharedValues } from 'some/path/shared-values.service';
bootstrap(AppComponent, [
// ...
SharedValues,
]);
无论你想在哪里使用它
import { SharedValues } from 'some/path/shared-values.service';
// or wherever you want to use the variables
@Component({
// ...
}) export class SomeComponent {
constructor(private shared: SharedValues) {
console.log(this.shared.Title, this.shared.Something);
}
}