gpt4 book ai didi

angular - 通过服务器上的 Http 请求的 URL 必须是绝对的。网址 : ./assets/i18n/en.json

转载 作者:太空狗 更新时间:2023-10-29 17:38:03 25 4
gpt4 key购买 nike

我是 Angular 2 的新手,我在 ngx-translate 组件中遇到以下问题(通过服务器上的 Http 请求的 URL 必须是绝对的。URL:./assets/i18n/en.json)。我确定这个 en.json 文件在那里,因为我向它发出 http 请求并且请求成功

这是我的 app.module.server.ts 文件:

import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { sharedConfig } from './app.module.shared';
import { HttpModule, Http } from '@angular/http';
import {
TranslateModule,
TranslateLoader,
MissingTranslationHandler,
MissingTranslationHandlerParams
} from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';

export function createTranslateLoader(http: Http) {
return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}

export class MyMissingTranslationHandler implements MissingTranslationHandler {
handle(params: MissingTranslationHandlerParams) {
return '[' + params.key + ']';
}
}

@NgModule({
bootstrap: sharedConfig.bootstrap,
declarations: sharedConfig.declarations,
imports: [
ServerModule,
HttpModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [Http]
}
}),
...sharedConfig.imports
]
})
export class AppModule {
}

这是我的 app.module.cleint.ts 文件:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule,Http } from '@angular/http';
import { sharedConfig } from './app.module.shared';
import {
TranslateModule,
TranslateLoader,
MissingTranslationHandler,
MissingTranslationHandlerParams
} from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
export function createTranslateLoader(http: Http) {
return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}

export class MyMissingTranslationHandler implements MissingTranslationHandler {
handle(params: MissingTranslationHandlerParams) {
return '[' + params.key + ']';
}
}

@NgModule({
bootstrap: sharedConfig.bootstrap,
declarations: sharedConfig.declarations,
imports: [
BrowserModule,
FormsModule,
HttpModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [Http]
}
}),
...sharedConfig.imports
],
providers: [
{ provide: 'ORIGIN_URL', useValue: location.origin },
{
provide: MissingTranslationHandler,
useClass: MyMissingTranslationHandler
}
]
})
export class AppModule {
}

这是我的 app.module.shared.ts 文件:

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { HttpModule, Http } from "@angular/http";
import { BrowserModule } from "@angular/platform-browser";

import { TranslateModule} from '@ngx-translate/core';

import { AppComponent } from './components/app/app.component'
import { NavMenuComponent } from './components/navmenu/navmenu.component';
import { HomeComponent } from './components/home/home.component';
import { FetchDataComponent } from './components/fetchdata/fetchdata.component';
import { EmployeesListComponent } from './components/employees/EmployeesList.component';
import { CounterComponent } from './components/counter/counter.component';

export const sharedConfig: NgModule = {
bootstrap: [AppComponent],
declarations: [
AppComponent,
NavMenuComponent,
CounterComponent,
FetchDataComponent,
EmployeesListComponent,
HomeComponent
],
imports: [
RouterModule.forRoot([
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'counter', component: CounterComponent },
{ path: 'fetch-data', component: FetchDataComponent },
{ path: 'employees-list', component: EmployeesListComponent },
{ path: '**', redirectTo: 'home' }
]),
BrowserModule,
HttpModule,TranslateModule
]
, exports: [
TranslateModule
]
};

最佳答案

问题就是这个。 ./assets/i18n/en.json 不是像这样的绝对 url:http://foo.com/assets/i18n/en.json .我知道通常有一些方法可以表明您是从与客户端代码相同的服务器提供服务的。难道不是吗

export function createTranslateLoader(http: Http) {
return new TranslateHttpLoader(http, '/assets/i18n/', '.json');
}

但是当我在 Angular 文档 repo 中四处寻找时:我看到了这个:https://github.com/angular/angular/issues/15349 .因此,angular http 服务可能不支持调用提供文档的主机。这有点令人惊讶,但您可以在 javascript 中使用:

window.location.origin

因此,当您设置调用时,只需将其添加到/assets/i18n/即可。

关于angular - 通过服务器上的 Http 请求的 URL 必须是绝对的。网址 : ./assets/i18n/en.json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44600108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com