有没有办法将后端呈现的参数传递给angular2 bootstrap方法?我想为使用BaseRequestOptions的所有请求设置http标头,并使用后端提供的值.我的main.ts文件看起来像这样:
import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppComponent } from "./app.component.ts";
bootstrap(AppComponent);
Run Code Online (Sandbox Code Playgroud)
我发现如何将这个参数传递给root组件(/sf/answers/2488755531/),但是当我开火bootstrap方法时我需要它......有什么想法吗?
编辑:
webpack.config.js内容:
module.exports = {
entry: {
app: "./Scripts/app/main.ts"
},
output: {
filename: "./Scripts/build/[name].js"
},
resolve: {
extensions: ["", ".ts", ".js"]
},
module: {
loaders: [
{
test: /\.ts$/,
loader: 'ts-loader'
}
]
}
};
Run Code Online (Sandbox Code Playgroud) 当angular 2 app被引导时,我们想调用一个rest api,我的意思是应该对应用程序做的第一件事就是调用这个api并获取应用程序所需的一些数据.
反正有没有实现这个目标?我看到了以下文章,但它是用于Angular 2的beta版本
我知道提供者是从另一个类获得服务但是什么是多提供者和令牌的东西?
还有什么时候multi=true呢?
provide(NG_VALIDATORS, { useExisting: class), multi: true })
Run Code Online (Sandbox Code Playgroud) angular2-forms angular2-directives angular2-services angular
我正在帮助使用webpack开发angular2 web应用程序,我们当前的计划是打开某种文件来配置我们的应用程序的功能(可能是.json).这主要是为了让人们实现我们的应用程序一个易于定位的文件,以指定以下内容:他们的API位于何处,是否要添加自定义样式表等.
我用来加载配置文件的服务如下所示:
//config.ts
@Injectable()
export class Config {
private static _config: Object;
private static _promise: Promise<any>;
constructor(private http: Http) {
Config._promise = this.load();
}
load() {
return new Promise((resolve, reject) => {
this.http.get(`./config/development.json`)
.map((response: Response) => response.json())
.catch((error: any) => {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
})
.subscribe((data) => {
Config._config = data;
resolve(true);
});
});
}
get(key: any) {
return Config._config[key];
}
}
Run Code Online (Sandbox Code Playgroud)
我在另一个服务类中使用它:
//api.service.ts
@Injectable()
export class ApiService {
private URI: string;
constructor(private http: Http, private …Run Code Online (Sandbox Code Playgroud) 我尝试使用异步rest调用中的值在“ CoreModule”中设置APP_BASE_HREF。我看不到如何完成此操作,因为provide方法需要返回一个字符串。
例如:
@NgModule({
imports: [
...
HttpModule
],
...
providers: [
...
...
BackendRequestClass,
{ provide: APP_BASE_HREF, useFactory: () => () => return '/some/path', deps: [], multi: true }
],
});
Run Code Online (Sandbox Code Playgroud)
但是当我需要Web服务中的值时,无法返回该字符串。任何想法如何做到这一点?
谢谢
在我们的 Angular 应用程序中,我们需要在初始化应用程序之前从服务器加载配置设置。
使用APP_INITIALIZER如这样伟大的作品:
export function loadConfig(config: ConfigService) => () => config.load()
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule,
routes,
FormsModule,
HttpModule],
providers: [
ConfigService,
{
provide: APP_INITIALIZER,
useFactory: loadConfig,
deps: [ConfigService],
multi: true
}
],
bootstrap: [AppComponent]
})
export class AppModule {
}
Run Code Online (Sandbox Code Playgroud)
然后我扩展了 ErrorHandler 来创建一个 GlobalErrorHandler 并想告诉 Angular 使用我们的 GlobalErrorHandler,所以我提供了 ErrorHandler 并告诉 Angular 使用类 GlobalErrorHandler。
providers: [
ConfigService,
{
provide: APP_INITIALIZER,
useFactory: loadConfig,
deps: [ConfigService],
multi: true
},
{
provide: ErrorHandler,
useClass: GlobalErrorHandler
}
]
Run Code Online (Sandbox Code Playgroud)
GlobalErrorHandler …