Angular2路由器模板请求http标头

时间:2016-08-20 21:07:21

标签: angular angular2-routing angular2-template

简单的问题。

Angular2路由器用于向服务器发出路由请求的内容是什么?我需要在所有模板请求中插入自定义标头(X-locale:' en'),以便服务器可以翻译它们。

我已经覆盖了BaseRequestOptions和Http类,它正在处理除默认路由调用之外的所有事情。我的意思是@Component模板调用。

更新#1

我将说明问题

以下是angular2对我的Laravel5后端的模板请求。

enter image description here

注意   Main.Users.user   Main.Users.user细节

等等。

这些是Angular2发出的所有@Component模板请求。我还会在下面发布请求详情

enter image description here

我需要将X-locale标头插入@Component模板请求的http标头中。为此,我需要知道哪些类创建了这些请求,因此我可以以某种方式修改请求标头。

更新#2

我还会发布自定义的BaseRequestOptions代码

void register_new() {

    system("clear");

    reg buffer;

    FILE *fp;
    fp = fopen("data.bin", "ab");

    if (fp == NULL) {
        error_message("Error opening file data.bin");
        exit(0);
    }

    write_register(buffer);

    fwrite(&buffer, sizeof(reg), 1, fp);

    fclose(fp);
}

这可以按预期工作,并将X-locale插入到我在应用程序中生成的所有Http请求中,但该死的模板调用除外。我也覆盖了Http类,但@Component模板调用既不调用Http类,也不调用BaseRequestOptions类。

Http.post请求的小例子

enter image description here

1 个答案:

答案 0 :(得分:0)

谢谢JB Nizet和Günter。我最后选择使用angular2-cookie而不是请求标题。

我做了一个全球CookieService服务,我为我的应用提供了这个服务,并设置了所有模板请求中使用的语言环境Cookie。

App.ts

import { CookieService }                                            from 'angular2-cookie/core';

//enableProdMode();
bootstrap(
    AppComponent,
[
    CookieService,

    ROUTER_PROVIDERS,
    ROUTER_DIRECTIVES,
    HTTP_PROVIDERS,

]
)  
.catch(err => console.error(err));

请求

enter image description here