我正在尝试为属性创建get和set方法:
private _name: string;
Name() {
get:
{
return this._name;
}
set:
{
this._name = ???;
}
}
Run Code Online (Sandbox Code Playgroud)
设置值的关键字是什么?
我正在使用Angular2 RC5.我希望能够在我的应用程序启动之前加载我的服务器IP地址和一些其他配置参数.我怎样才能在最新的RC5中做到这一点?
我看过其他文章,但它们没有帮助:
任何帮助将不胜感激.
编辑
我尝试在app.module.ts中使用APP_INITIALIZER,如下所示:
import { NgModule, provide, APP_INITIALIZER } from "@angular/core";
import { ConfigService } from "./shared/services/config.service";
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule,
routes,
FormsModule,
HttpModule],
providers: [AuthService,
Title,
appRoutingProviders,
ConfigService],
bootstrap: [AppComponent
, provide(APP_INITIALIZER,
{
useFactory: (config: ConfigService) => () => config.load(),
deps: [ConfigService], multi: true
})]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
这是我的config.service.ts文件:
import { Config } from "../model/config";
export class ConfigService {
constructor() {
}
load(): Promise<Config> {
let config: Config …Run Code Online (Sandbox Code Playgroud) 我HTTP_INTERCEPTORS在angular4中使用.为此,我创建HttpServiceInterceptor了实现HttpInterceptor接口的类并为intercept方法提供了定义.然后注册提供商HTTP_INTERCEPTORS这样
providers: [{
provide: HTTP_INTERCEPTORS,
useClass: HttpServiceInterceptor,
multi: true
}],
Run Code Online (Sandbox Code Playgroud)
这工作正常.但我仍然不明白multi:true这里的意思是什么?我读过这个答案.
如果我删除multi:true选项,则浏览器端出现错误
Uncaught Error: Provider parse errors:
Mixing multi and non multi provider is not possible for token InjectionToken_HTTP_INTERCEPTORS ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1
at NgModuleProviderAnalyzer.webpackJsonp.487.NgModuleProviderAnalyzer.parse (vendor.js:36335)
at NgModuleCompiler.webpackJsonp.487.NgModuleCompiler.compile (vendor.js:43184)
at JitCompiler.webpackJsonp.487.JitCompiler._compileModule (vendor.js:51527)
at vendor.js:51472
at Object.then (vendor.js:26354)
at JitCompiler.webpackJsonp.487.JitCompiler._compileModuleAndComponents (vendor.js:51470)
at JitCompiler.webpackJsonp.487.JitCompiler.compileModuleAsync (vendor.js:51399)
at PlatformRef_.webpackJsonp.0.PlatformRef_._bootstrapModuleWithZone (vendor.js:4746)
at PlatformRef_.webpackJsonp.0.PlatformRef_.bootstrapModule (vendor.js:4732)
at Object.<anonymous> …Run Code Online (Sandbox Code Playgroud)