【问题标题】:ngx-translate translating in all children components of the modulengx-translate 在模块的所有子组件中进行翻译
【发布时间】:2019-10-02 02:45:49
【问题描述】:

我目前在angular 7上使用ngx-translate。我的项目正在翻译两种语言,英文和中文,该项目由如下所示的结构组成,A.component是父组件,B.component是子组件。

模块 A

    {
      componentB:{
         B.component.ts
         ....
      },
      A.component.ts
      A.module.ts
      .... 
    }

导出类 BComponent 实现 OnInit{

  constructor(private translate: TranslateService
              ) {
        translate.addLangs(['zh-CN', 'en']);
        translate.use('zh-CN');
  }
}

上面的代码正在翻译。

但是如果上面的代码只添加到A.component而不是B.component。翻译不会发生在 B.component 上。

不知道有没有办法将代码添加到模块的父组件中,并且以下所有子组件都可以自动翻译,而无需将代码放在每个子组件中。

【问题讨论】:

  • 您找到解决方案了吗? @user824624

标签: angular ngx-translate


【解决方案1】:

根据您的项目结构(如果组件 B 有自己的模块,我假设),您可能需要在遵循文档中所述的设置后在您的 Component B 模块导入中添加 TranslateModule

@NgModule({
  imports: [ 
    TranslateModule,
    ...
  ],
  ...
})
export class ComponentB {}

【讨论】:

    猜你喜欢
    • 2021-04-22
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多