1

我对 ionic 3 和 Angular 4 非常陌生。我正在尝试翻译页面,但是当我运行应用程序时出现此错误。我添加了库并按照文档所述导入了所有内容,并在 app 模块的 providers 数组中添加了翻译服务,但我仍然收到此错误

在此处输入图像描述

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import {HttpClientModule, HttpClient} from '@angular/common/http';

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

import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import {TranslateModule, TranslateLoader, TranslateService} from '@ngx-translate/core';
import {TranslateHttpLoader} from '@ngx-translate/http-loader';

export function HttpLoaderFactory(http: HttpClient) {
  return new TranslateHttpLoader(http, "./assets/i18n/", ".json");
}

@NgModule({
  declarations: [
    MyApp
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp),
    HttpClientModule,
    TranslateModule.forRoot({
        loader: {
            provide: TranslateLoader,
            useFactory: HttpLoaderFactory,
            deps: [HttpClient]
        }
    })
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler}, 
    TranslateService

  ]
})
export class AppModule {}

app.components.ts

import { Component, ViewChild,Inject, Injectable} from '@angular/core';
import { Nav, Platform} from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

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

@Injectable()
@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  @ViewChild("myNav") nav: Nav;

  rootPage: any;
  pages: Array<{title: string, component: any, icon: string}>;

  constructor(public platform: Platform,
              public statusBar: StatusBar,
              public splashScreen: SplashScreen ,
              public translate: TranslateService) {

    // this language will be used as a fallback when a translation isn't 
    // found in the current language
    translate.setDefaultLang('en');
    translate.use('en');

    platform.ready().then(() => {
       // Okay, so the platform is ready and our plugins are available.
       // Here you can do any higher level native things you might need.
       statusBar.styleDefault();
       splashScreen.hide();
    });
  }

  switchLanguage(language: string){
    this.translate.use(language);
  }
}

主页.ts

import { Component } from '@angular/core';
import { NavController, NavParams, Platform } from 'ionic-angular';

@IonicPage()
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage{

constructor(public navCtrl: NavController,
    private platform: Platform,
    private navParams: NavParams){}

}

home.module.ts

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { HomePage} from './home';
@NgModule({
  declarations: [
    HomePage
  ],
  imports: [
    IonicPageModule.forChild(HomePage)
  ],
})
export class HomePageModule {}

我还在“assets/i18n/”中添加了文件夹和 2 个 json 文件。
请需要帮助!

4

1 回答 1

3

对于角度版本 < 4.3 需要安装这个版本http-loader@0.1.0的 http-loader

1)npm install @ngx-translate/http-loader@0.1.0 --save

2)npm install @ngx-translate/core --save

3) 从@angular/http 导入HttpModule 和Http

4) 从@ngx-translate/core 导入 TranslateModule、TranslateLoader、TranslateService

5) 从@ngx-translate/http-loader 导入 TranslateHttpLoader

6) app.module.ts中的导出函数,参数为 Http

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

这是解决函数参数问题的方法,因为我使用的是最新版本的http-loader,并且我调用了httpClientModule & HttpClient,它与角度旧版本不兼容。

7) 最后但并非最不重要的初始化对象在调用服务TranslateService的构造函数中

public constructor(public translate: TranslateService){

}

8)最后你可以使用这个在视图(html页面)的构造函数中初始化它的对象,如下所示:

 {{'HOME.HELLO' | translate }} 

注意:在 json 文件中,字符串(键和值)必须全部大写。

于 2017-11-08T23:12:57.687 回答