ngx-translate с ionic 3 не работает - PullRequest
0 голосов
/ 07 июня 2018

Я создаю многоязычное приложение в ionic 3 с использованием ngx-translate.ниже мой код

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { TranslateModule, TranslateLoader } from "@ngx-translate/core";
import { TranslateHttpLoader } from "@ngx-translate/http-loader";
import { HttpClient, HttpClientModule } from "@angular/common/http"; 

import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { ListPage } from '../pages/list/list';

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

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

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

app.component.ts

import { Component, ViewChild } 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';

import { HomePage } from '../pages/home/home';
import { ListPage } from '../pages/list/list';

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

  rootPage: any = HomePage;

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

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

      translate.setDefaultLang('en');
      translate.use('en');
      this.initializeApp();

    // used for an example of ngFor and navigation
    this.pages = [
      { title: 'Home', component: HomePage },
      { title: 'List', component: ListPage }
    ];

  }

  initializeApp() {
    this.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.
      this.statusBar.styleDefault();
      this.splashScreen.hide();
    });
  }

  openPage(page) {
    // Reset the content nav to have just this page
    // we wouldn't want the back button to show in this scenario
    this.nav.setRoot(page.component);
  }
}

это простая реализация ngx-translate.я получаю следующую ошибку

Runtime Error
Function Expected

TypeError: Function expected
   at TranslateService.prototype.get (http://localhost:8100/build/vendor.js:80892:17)
   at TranslatePipe.prototype.updateValue (http://localhost:8100/build/vendor.js:81287:9)
   at Anonymous function (http://localhost:8100/build/vendor.js:81357:21)
   at Anonymous function (http://localhost:8100/build/vendor.js:5039:36)
   at SafeSubscriber.prototype.__tryOrUnsub (http://localhost:8100/build/vendor.js:20899:13)
   at SafeSubscriber.prototype.next (http://localhost:8100/build/vendor.js:20846:17)
   at Subscriber.prototype._next (http://localhost:8100/build/vendor.js:20786:9)
   at Subscriber.prototype.next (http://localhost:8100/build/vendor.js:20750:13)
   at Subject.prototype.next (http://localhost:8100/build/vendor.js:23237:17)
   at EventEmitter.prototype.emit (http://localhost:8100/build/vendor.js:5007:24)

Ionic Framework: 3.9.2 Сценарии Ionic App: 3.1.9 Angular Core: 5.2.10 CLI углового компилятора: 5.2.10 Узел: 7.8.0

любая помощь

1 Ответ

0 голосов
/ 07 июня 2018

Я решил проблему.Проблема была с версией @ ngx-translate.Я использовал @ ngx-translate версии 10.0 с углом 5, который не поддерживается, как описано здесь .Я использовал @ ngx-translate версии 9.x и @ ngx-translate / http-loader версии 2.x.Это решило мою проблему.

...