首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ngx-translate/core "Error: No provider for HttpClient!“

ngx-translate/core "Error: No provider for HttpClient!“
EN

Stack Overflow用户
提问于 2017-08-23 02:47:34
回答 1查看 10.2K关注 0票数 14

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

我不能让翻译正常工作。我采取的步骤如下:

1]定义AppModule中的所有内容

代码语言:javascript
复制
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中的所有内容

代码语言:javascript
复制
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

代码语言:javascript
复制
<div>{{ 'HELLO' | translate:param }}</div>

4]并最终在assets/i18n/en.json中创建

代码语言:javascript
复制
{
    "HELLO": "Hi There"
}

我一直在下面的屏幕截图中看到这些错误

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-23 03:02:42

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

代码语言:javascript
复制
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?

票数 35
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45824854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档