在离子3.9.2下, ngx-translate(v9)与延迟加载有一些困难。在我运行" buid --prod"之前,一切都在浏览器中正常工作。构建过程的npc部分(我认为它是AOT编译)报告了一个"未知标识符" TranslateLoader和TranslatePipe。
虽然我已经按照ngx tutorial by ionic阅读了ngx-translate和StackOverflow的github,但我还没有找到类似问题的讨论。因此,我想寻求帮助。
运行 ionic cordova build ios --prod 时出现以下错误(请注意第一行,显示TranslateLoader和TranslatePipe的问题):
Error: Internal error: unknown identifier [{"filePath":"..../node_modules/@ngx-translate/core/core.d.ts","name":"TranslateLoader","members":[]},{"filePath":"..../node_modules/@ngx-translate/core/core.d.ts","name":"TranslatePipe","members":[]}]
at Object.importExpr$$1 [as importExpr] (..../node_modules/@angular/compiler/bundles/compiler.umd.js:31063:23)
at tokenExpr (..../node_modules/@angular/compiler/bundles/compiler.umd.js:20063:39)
at providerDef (..../node_modules/@angular/compiler/bundles/compiler.umd.js:19966:20)
at ..../node_modules/@angular/compiler/bundles/compiler.umd.js:20188:77
at Array.map (<anonymous>)
at NgModuleCompiler.compile (..../node_modules/@angular/compiler/bundles/compiler.umd.js:20188:44)
at AotCompiler._compileModule (..../node_modules/@angular/compiler/bundles/compiler.umd.js:30956:32)
at ..../node_modules/@angular/compiler/bundles/compiler.umd.js:30838:66
at Array.forEach (<anonymous>)
at AotCompiler._compileImplFile (..../node_modules/@angular/compiler/bundles/compiler.umd.js:30838:19)
[11:14:07] copy finished in 9.27 s
这是我的app.module.ts
...
import { HttpClient, HttpClientModule } from "@angular/common/http";
import { TranslateLoader, TranslateModule } from "@ngx-translate/core";
import { TranslateHttpLoader } from "@ngx-translate/http-loader";
...
@NgModule({
declarations: [
MyApp,
],
imports: [
TranslateModule.forRoot({
loader: {
provide: [TranslateLoader],
useFactory: (HttpLoaderFactory),
deps: [HttpClient]
}
}),
IonicModule.forRoot(MyApp,{
preloadModules: true,
}),
BrowserModule,
HttpClientModule,
BrowserAnimationsModule,
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
],
providers: [SplashScreen, Keyboard, StatusBar, {provide: ErrorHandler, useClass: IonicErrorHandler}, AuthService]
})
...
的package.json:
"@ngx-translate/core": "^9.1.1",
"@ngx-translate/http-loader": "^2.0.1", // 3rd version gives the same error.
然后我在应用程序中将ngx-translate作为.forChild({...})导入,并使用相同的导出函数HttpLoaderFactory()。
最奇怪的是,当我从
更改启动时 TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (HttpLoaderFactory),
deps: [HttpClient]
}
}),
to(将TranslateLoader作为数组提供)
TranslateModule.forRoot({
loader: {
provide: [TranslateLoader],
useFactory: (HttpLoaderFactory),
deps: [HttpClient]
}
}),
错误信息越来越短,TranslatePipe的问题就消失了:
Error: Internal error: unknown identifier [{"filePath":".../node_modules/@ngx-translate/core/core.d.ts","name":"TranslateLoader","members":[]}]
at Object.importExpr$$1 [as importExpr] (.../node_modules/@angular/compiler/bundles/compiler.umd.js:31063:23)
at tokenExpr (.../node_modules/@angular/compiler/bundles/compiler.umd.js:20063:39)
at providerDef (.../node_modules/@angular/compiler/bundles/compiler.umd.js:19966:20)
at .../node_modules/@angular/compiler/bundles/compiler.umd.js:20188:77
at Array.map (<anonymous>)
at NgModuleCompiler.compile (.../node_modules/@angular/compiler/bundles/compiler.umd.js:20188:44)
at AotCompiler._compileModule (.../node_modules/@angular/compiler/bundles/compiler.umd.js:30956:32)
at .../node_modules/@angular/compiler/bundles/compiler.umd.js:30838:66
at Array.forEach (<anonymous>)
at AotCompiler._compileImplFile (.../node_modules/@angular/compiler/bundles/compiler.umd.js:30838:19)
你能帮忙吗?
祝你好运
的Jakub
答案 0 :(得分:0)
好的,我明白了。我在我的所有页面和组件中都使用它来启动TranslateModule:
TranslateModule.forChild({
loader: {
provide: [TranslateLoader],
useFactory: (HttpLoaderFactory),
deps: [HttpClient]
}
}),
我把它更改为:
TranslateModule.forChild()
它正在发挥作用。
也许这会对某人有所帮助。