我正在制作一个新的angular2应用程序,它将在我们所有服务器上的所有应用程序上点击一个端点以获取内部版本号,然后它将显示在网格中,以便我们看到服务器具有哪些内部编号。Angular2从配置文件中读取
当我使用Angular1构建类似的应用程序时,我能够使用gulp-ng-config来读取JSON文件并使用JSON中的值输出角度模块。我期待在Angular2中做类似的事情,但我不知道如何做到这一点。有任何想法吗?
我正在制作一个新的angular2应用程序,它将在我们所有服务器上的所有应用程序上点击一个端点以获取内部版本号,然后它将显示在网格中,以便我们看到服务器具有哪些内部编号。Angular2从配置文件中读取
当我使用Angular1构建类似的应用程序时,我能够使用gulp-ng-config来读取JSON文件并使用JSON中的值输出角度模块。我期待在Angular2中做类似的事情,但我不知道如何做到这一点。有任何想法吗?
这样,你去阅读.json
文件中Angular2
。
注意:这样您可以读取.json
文件。此演示显示.json
文件示例。
working demo =>点击friends tab
http.get('friends.json (=filepath)')
.map(res => res.json())
.subscribe((data) => { this.result=data; },
err=>console.log(err),
()=>console.log('done')
);
正常$ http获取调用配置文件应该满足您的要求。
$http.get('path-to-configuration-file')
.success(function (confData) {
// Use your conf data however you like
})
.error(function() {
// File not found
});
我希望这个作品
虽然吨帽子实际上是有道理的,你不是说'Http'?我不认为$ http是在angular2 –
@AlexKibler:感谢您的更正。 Http就是我的意思。对angular2不太熟悉,但作为一个想法http应该为这个问题工作 – ganeshwani
的最佳方式来读取配置文件中Angular2发行版本2.1.0.0是什么样子,你的服务会像下面
@Injectable()
export class ConfigService {
private Server:string = "";
private AppKey = "";
constructor(private _http:Http) {
}
public load() {
return new Promise((resolve, reject) => {
this._http.get('config.json') // path of your config.json file
.map(res => res.json())
.subscribe(
(data:any) => {
this.Server = data.server;
this.AppKey = data.appkey;
resolve(true);
},
err=>console.log(err)
);
});
}
public getServer(){
return this.Server;
}
public getAppKey(){
return this.AppKey;
}
}
你必须加载此配置app.module.ts像
@NgModule({
declarations: [
..
],
imports: [
...
],
providers: [ConfigService,
{
provide: APP_INITIALIZER,
useFactory: (config:ConfigService) =>() => config.load(),
deps: [ConfigService],
multi: true
}
],
bootstrap: [AppComponent]
})
export class AppModule {
}
感谢一如既往micronyks! –
不客气@Alex Kibler !!! – micronyks