多语言@ ngx-translate Ionic 3对象(...)不是TranslateService.get问题的函数?

时间:2018-04-24 07:14:06

标签: angular ionic-framework multilingual

我想实现多语言功能。我跟着https://ionicframework.com/docs/developer-resources/ng2-translate/。但我得到的对象(...)不是TranslateService.get错误的函数

我有搜索谷歌这个错误,但没有运气。我遵循了https://codesundar.com/lesson/ionic-multi-language-support-i18n/

这是代码

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';

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

import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { HttpClientModule, HttpClient } from '@angular/common/http';

// If I use this I get error !! Argument of type 'Http' is not assignable to parameter of type 'HttpClient'. Property 'handler' is missing in type 'Http'.
// import { HttpModule, Http } from '@angular/http';

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

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

home.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { TranslateService } from '@ngx-translate/core';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  lang:any;
  constructor(public navCtrl: NavController, public translate: TranslateService) {
    this.lang = 'en';
    this.translate.setDefaultLang('en');
    this.translate.use('en');
  }

  switchLanguage() {
    this.translate.use(this.lang);
  }

}

home.html的

<ion-header>
  <ion-navbar>
    <ion-title>
      Ionic Blank
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>

  <ion-list>
    <ion-item>
      <ion-label>Language</ion-label>
      <ion-select [(ngModel)]="lang" (ionChange)="switchLanguage()">
        <ion-option value="en">English</ion-option>
        <ion-option value="ta">Tamizh (தமிழ்)</ion-option>
      </ion-select>
    </ion-item>
  </ion-list>

  <ion-list>
    <ion-item>
      <h2>{{ 'APPLE' | translate }}</h2>
    </ion-item>
    <ion-item>
      <h2>{{ 'BALL' | translate }}</h2>
    </ion-item>
    <ion-item>
      <h2>{{ 'CAT' | translate }}</h2>
    </ion-item>
  </ion-list>
</ion-content>

的package.json

{
  "name": "ionLang",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "http://ionicframework.com/",
  "private": true,
  "scripts": {
    "clean": "ionic-app-scripts clean",
    "build": "ionic-app-scripts build",
    "lint": "ionic-app-scripts lint",
    "ionic:build": "ionic-app-scripts build",
    "ionic:serve": "ionic-app-scripts serve"
  },
  "dependencies": {
    "@angular/animations": "5.2.10",
    "@angular/common": "5.2.10",
    "@angular/compiler": "5.2.10",
    "@angular/compiler-cli": "5.2.10",
    "@angular/core": "5.2.10",
    "@angular/forms": "5.2.10",
    "@angular/http": "5.2.10",
    "@angular/platform-browser": "5.2.10",
    "@angular/platform-browser-dynamic": "5.2.10",
    "@ionic-native/core": "4.7.0",
    "@ionic-native/splash-screen": "4.7.0",
    "@ionic-native/status-bar": "4.7.0",
    "@ionic/storage": "2.1.3",
    "@ngx-translate/core": "^10.0.1",
    "@ngx-translate/http-loader": "^3.0.1",
    "ionic-angular": "3.9.2",
    "ionicons": "3.0.0",
    "rxjs": "5.5.10",
    "sw-toolbox": "3.6.0",
    "zone.js": "0.8.26"
  },
  "devDependencies": {
    "@ionic/app-scripts": "3.1.9",
    "typescript": "~2.6.2"
  },
  "description": "An Ionic project"
}

ERROR enter image description here

请帮忙。感谢

3 个答案:

答案 0 :(得分:5)

此错误是因为您使用角度v5与最新的ngx-translate版本10,它与angular v5不兼容。对于使用角度为v5的ngx-translate,您需要使用ngx-translate 9.x版本,错误将消失。你可以在@ngx-translate/core GitHub repo中找到它,他们已经清楚地提到了Object的原因(...)不是函数错误

答案 1 :(得分:5)

删除最新的ngx-translate并安装其旧版本。

  • 使用命令删除@ ngx-translate / core(10.0.1) npm删除@ ngx-translate / core
  • 然后通过命令安装@ ngx-translate / core的9.1.1版本 npm install @ ngx-translate / core @ 9.1.1 --save

答案 2 :(得分:0)

  

首先通过以下命令删除您的插件

npm remove @ngx-translate/core @ngx-translate/http-loader --save
  

然后安装插件

npm install @ngx-translate/core@9.1.1 @ngx-translate/http-loader@2.0.1  --save

更多细节

选择与您的Angular版本相对应的版本:

Angular      || @ngx-translate/core || @ngx-translate/http-loader

7/8          || 11.x+               || 4.x+
6            || 10.x                || 3.x
5            || 8.x to 9.x          || 1.x to 2.x
4.3          || 7.x or less         ||  1.x to 2.x
2 to 4.2.x   || 7.x or less         || 0.x