我即将在Ionic3应用程序中使用Lazy加载实现语言规范化。我正在使用插件ngx-translate
。但我收到错误'TypeError: Cannot read property 'call' of undefined'
。请帮我解决这个错误
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { HttpClient } from "@angular/common/http";
export function createTranslateLoader(https: HttpClient) {
return new TranslateHttpLoader(https, './assets/i18n/', '.json');
}
@NgModule({
declarations: [
MyApp
],
imports: [
BrowserModule,
HttpModule,
IonicModule.forRoot(MyApp),
AngularFireModule.initializeApp(config),
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [Http]
}
})
],
bootstrap: [IonicApp],
entryComponents: [
MyApp
],
providers: [
-------
]
})
export class AppModule { }
ngOnInit(): void {
this.translate.addLangs(["en", "ml"]);
this.defaultLng = this.translate.setDefaultLang("ml");
this.translate.use(this.translate.getBrowserLang().match(/en|ml/) ? this.translate.getBrowserLang() : this.defaultLng);
}
import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { LoginPage } from './login';
import { Http } from '@angular/http';
import { TranslateModule } from '@ngx-translate/core';
@NgModule({
declarations: [
LoginPage,
],
imports: [
IonicPageModule.forChild(LoginPage),
TranslateModule.forChild()
],
})
export class LoginPageModule {}
<ion-item no-lines>
<span >{{'language'|translate}}:</span> <span (click)="changeLanguage('en')">english</span>
<span (click)="changeLanguage('sp')">Spanish</span>
</ion-item>
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'call' of undefined
TypeError: Cannot read property 'call' of undefined
at __webpack_require__ (vendor.js:55)
at main.js:765
at t.invoke (polyfills.js:3)
at Object.onInvoke (vendor.js:5291)
at t.invoke (polyfills.js:3)
at r.run (polyfills.js:3)
at polyfills.js:3
at t.invokeTask (polyfills.js:3)
at Object.onInvokeTask (vendor.js:5282)
at t.invokeTask (polyfills.js:3)
at __webpack_require__ (vendor.js:55)
at main.js:765
at t.invoke (polyfills.js:3)
at Object.onInvoke (vendor.js:5291)
at t.invoke (polyfills.js:3)
at r.run (polyfills.js:3)
at polyfills.js:3
at t.invokeTask (polyfills.js:3)
at Object.onInvokeTask (vendor.js:5282)
at t.invokeTask (polyfills.js:3)
at c (polyfills.js:3)
at Object.reject (polyfills.js:3)
at NavControllerBase._fireError (vendor.js:55465)
at NavControllerBase._failed (vendor.js:55458)
at vendor.js:55505
at t.invoke (polyfills.js:3)
at Object.onInvoke (vendor.js:5291)
at t.invoke (polyfills.js:3)
at r.run (polyfills.js:3)
at polyfills.js:3
答案 0 :(得分:1)
最后我找到了解决方案。请检查以下对问题中发布的代码的更改。
MissingTranslationHandler
@ngx-translate/core
导出名为MyMissingTranslationHandler
的自定义类,如下所示。并在app.module.ts
import {MissingTranslationHandler, MissingTranslationHandlerParams} from '@ngx-translate/core'
;
export class MyMissingTranslationHandler implements MissingTranslationHandler {
handle(params: MissingTranslationHandlerParams) {
console.log(params);
return 'Translations not available for ' + params.key;
}
}
import { HttpClient,HttpClientModule } from "@angular/common/http";
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
添加函数HttpLoaderFactory
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http,'assets/i18n/', '.json');
}
import
的{{1}}数组中,将@NgModule
更改为 TranslateModule
7.在TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
数组中添加providers
在我们的.module.ts文件中,在{ provide: MissingTranslationHandler, useClass: MyMissingTranslationHandler}
数组中添加TranslateModule
而不是imports
。
如果您有任何疑问,请在评论部分提及。