在 Ionic 4 中显示找不到管道 'translate' 错误

The pipe 'translate' could not be found error is showing In Ionic 4

我正在使用我的 Ionic 4 应用程序,我已经安装了 ngx-translate 插件。它在 app.component.html 中工作正常,但在 tabs.page.html 中显示错误。

The pipe 'translate' could not be found

这是我的app.component.html:

<ion-list class="mylist22" color="myheader">
    <ion-item color="myheader">
        <ion-label>Gender</ion-label>
        <ion-select [(ngModel)]="languageSelected" (ionChange)='setLanguage()'>
            <ion-select-option value="en" selected>English</ion-select-option>
            <ion-select-option value="ar">Arabic</ion-select-option>
        </ion-select>
    </ion-item>
</ion-list>

在这个视图中,我有语言 select 框。

这是我的app.component.ts:

import { TranslateService } from '@ngx-translate/core';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html'
})
export class AppComponent {
  languageSelected: any = 'en';
  constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar,
    private translate: TranslateService
  ) {
    this.translate.addLangs(['en', 'ar']);
    this.translate.setDefaultLang('en');
    this.initializeApp();
  }

  initializeApp() {
    this.platform.ready().then(() => {
      this.statusBar.styleDefault();
      this.splashScreen.hide();

      this.setLanguage();
    });
  }

  setLanguage() {
    const defaultLanguage = this.translate.getDefaultLang();
    if (this.languageSelected) {
      console.log(this.languageSelected);
      this.translate.setDefaultLang(this.languageSelected);
      this.translate.use(this.languageSelected);
    } else {
      this.languageSelected = defaultLanguage;
      this.translate.use(defaultLanguage);
    }
  }
}

这是我的app.module.ts:

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

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

@NgModule({
  imports: [
    TranslateModule.forRoot({
      loader: {
          provide: TranslateLoader,
          useFactory: HttpLoaderFactory,
          deps: [HttpClient]
      }
  }) ],
})

app.component.html 中工作正常,但在 tabs.pahe.html 中不工作。

这是在tabs.page.html:

<ion-label>{{ 'ACCOUNT_TAB_LAB' | translate }}</ion-label>

Error: The pipe 'translate' could not be found.

非常感谢任何帮助。

您需要在每个要使用 translate 管道的模块中导入 TranslateModule

import { TranslateModule } from '@ngx-translate/core';

   ...
   imports: [
     TranslateModule // do not call forRoot from any module other than AppModule
   ] 
   ...

尝试将 TranslateModule 添加到 shared.module 文件:

import { TranslateModule } from "@ngx-translate/core";

@NgModule({
  imports: [TranslateModule, ],
  
  exports: [TranslateModule],
  providers: [],
})

export class SharedModule {}

就我而言,错误消息有点令人困惑。 在我的代码合并期间,组件从 app.module.ts declarations 中删除。将组件添加到 app.module.ts declarations.

后问题已解决