【问题标题】:Ionic 3, ngx-translate 9.x; build -> ngc: Internal error: unknown identifier TranslateLoader & TranslatePipe离子 3,ngx 翻译 9.x; build -> ngc: 内部错误:未知标识符 TranslateLoader & TranslatePipe
【发布时间】:2018-10-23 04:50:09
【问题描述】:

我在 ionic 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)

你能帮忙吗?

最好的问候

雅库布

【问题讨论】:

    标签: angular cordova ionic3 angular2-aot ngx-translate


    【解决方案1】:

    好的,我想通了。我在我的所有页面和组件中都使用它来启动 TranslateModule:

    TranslateModule.forChild({
          loader: {
            provide: [TranslateLoader],
            useFactory: (HttpLoaderFactory),
            deps: [HttpClient]
          }
        }),
    

    我改成这样了:

    TranslateModule.forChild()
    

    它正在工作。

    也许这会对某人有所帮助。

    【讨论】:

    • 我必须将此加载程序 forChild({...}) 添加回我更改语言的页面,否则翻译不会更新。它正在通过构建过程。
    猜你喜欢
    • 2018-03-15
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 2016-12-15
    相关资源
    最近更新 更多