NgModule的提供程序无效。仅允许Provider和Type的实例,得到:[?[object Object] ?, ...]

时间:2019-03-20 12:20:40

标签: angular

在我的应用中,我想介绍语言的国际化。所以我有2个文件'.json'和翻译。 我遇到了错误:

  

NgModule'TranslateModule'的无效提供程序-仅允许提供程序和类型的实例,得到:[?[object Object] ?, ...]

我看不到我在做什么错。这是app.module.ts

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {TranslateModule} from 'ng2-translate';

import {AppComponent} from './app.component';
import {NavbarComponent} from './components/navbar/navbar.component';
import {RouterModule} from '@angular/router';
import {appRoutes} from './app.route';
import {DoctorsComponent} from './pages/doctors/doctors.component';
import {HomeComponent} from './pages/home/home.component';
import {HospitalsComponent} from './pages/hospitals/hospitals.component';
import {HttpClientModule} from '@angular/common/http';
import {FormsModule} from '@angular/forms';
import {translateFactory} from './TranslateFactory.component';
import {TranslateLoader} from '@ngx-translate/core';


@NgModule({
  declarations: [
    AppComponent,
    NavbarComponent,
    HomeComponent,
    DoctorsComponent,
    HospitalsComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    FormsModule,
    RouterModule.forRoot(
      appRoutes,
      {enableTracing: false}
    ),
    TranslateModule.forRoot({
      loader: {
        provider: TranslateLoader,
        useClass: (translateFactory)
      }
    })
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

这是TranslateFactory.component.ts

import * as enTranslate from '../i18n/en.json';
import * as roTranslate from '../i18n/ro.json';
import { of, Observable } from 'rxjs';
import { TranslateLoader as NgxTranslateLoader } from '@ngx-translate/core';

const TRANSLATIONS = {
  en: enTranslate,
  ro: roTranslate
};

export class TranslateLoader implements NgxTranslateLoader {

  public getTranslation(lang: string): Observable<any> {
    return of(TRANSLATIONS[lang]);
  }
}

export function translateFactory() {
  return new TranslateLoader();
}

这是国际化语言的设置。

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {

  constructor(
    private translate: TranslateService
  ) {
    translate.addLangs(['ro', 'en']);
    translate.setDefaultLang('ro');
    const browserLang = translate.getBrowserLang();
    translate.use(browserLang.match(/ro|en/) ? browserLang : 'ro');
  }
}

您能提出一些解决方案还是告诉我我做错了什么?

2 个答案:

答案 0 :(得分:0)

TranslateModule.forRoot({
  loader: {
    provider: TranslateLoader,
    useClass: (translateFactory)
  }
})

使用useFactory代替useClass。 使用provide代替provider

import {TranslateModule} from 'ng2-translate';

使用@ngx-translate/core代替ng2-translate

答案 1 :(得分:0)

我找到了另一种可行的方法。

@ngx-translate/core;导入TranslateService和TranslateModule

在app.module.ts中使用HttpLoaderFactory和TranslateLoader:

TranslateModule.forRoot({
          loader: {
            provide: TranslateLoader,
            useFactory: HttpLoaderFactory,
            deps: [HttpClient ]
          }
        })


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

确保“ .json”文件位于/ assets / i18n中。这是获取json进行翻译的默认文件夹。