在Ionic 2应用程序中安装ng2-translate库

时间:2017-03-24 18:11:58

标签: angular ionic-framework ionic2

我正在尝试使用official doc在我的离子2应用中安装ng2-translate。但它不起作用。我不明白为什么官方指示不起作用。

由于我是初学者,因此我不会理解错误。

app.module.ts:

import { NgModule, ErrorHandler } from '@angular/core';
import { Http } from '@angular/http';
import { TranslateModule } from 'ng2-translate/ng2-translate';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { WeightlevelPage } from '../pages/weightlevel/weightlevel';
import { RecovolPage } from '../pages/recovol/recovol';
import { ComparePage } from '../pages/compare/compare';


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


@NgModule({
  declarations: [
    MyApp,
    WeightlevelPage,
    RecovolPage,
    ComparePage
  ],
  imports: [
    IonicModule.forRoot(MyApp),
    TranslateModule.forRoot(
      {
        provide: TranslateLoader,
        useFactory: (createTranslateLoader),
        deps: [Http]
      }
    )
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    WeightlevelPage,
    RecovolPage,
    ComparePage
  ],
  providers: [{provide: ErrorHandler, useClass: IonicErrorHandler}]
})
export class AppModule {}

我收到了这些错误:

Typescript Error
Cannot find name 'TranslateStaticLoader'.
src/app/app.module.ts

此行return new TranslateStaticLoader(http, 'assets/i18n', '.json')

Typescript Error
Cannot find name 'TranslateLoader'.
src/app/app.module.ts

这一行:provide: TranslateLoader,

1 个答案:

答案 0 :(得分:4)

在此处添加TranslateLoader

  

导入{TranslateModule,TranslateLoader}来自' ng2-translate / ng2-translate';

对于createTranslateLoader,请确保这两个文件夹assets/i18n位于src下。