gpt4 book ai didi

angular - ngx-翻译/核心 "Error: No provider for HttpClient!"

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

我已经下载了 ngx-translate/core 包,并按照文档说明进行操作。

我无法进行翻译。我做的步骤:

1] 在 AppModule 中定义一切

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { TranslateModule } from '@ngx-translate/core';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';

import { routing } from './app.routes';

import { AppComponent } from './app.component';

export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}

@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
routing,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

2] 在 AppComponent 中定义一切

import { Component } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: []
})
export class AppComponent {
param = { value: 'world' };

constructor(private router: Router, translate: TranslateService) {
// this language will be used as a fallback when a translation isn't found in the current language
translate.setDefaultLang('en');

// the lang to use, if the lang isn't available, it will use the current loader to get them
translate.use('en');
}
}

3] html

<div>{{ 'HELLO' | translate:param }}</div>

4] 最后创建在assets/i18n/en.json

{
"HELLO": "Hi There"
}

我在下面的屏幕截图中不断收到这些错误 Errors the popup in the browser console

我做错了什么?

最佳答案

ngx-translate/core 使用最新的 HttpClientModule 而不是旧的 HttpModule 的 imports 数组中更改以下内容NgModule

import { HttpClientModule } from "@angular/common/http";

imports: [
BrowserModule,
FormsModule,
HttpClientModule, // the change from http module
routing,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
]

有关详细信息,请参阅 Difference between HTTP and HTTPClient in angular 4?

关于angular - ngx-翻译/核心 "Error: No provider for HttpClient!",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45824854/

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