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

标签 angular ionic-framework ionic4 ngx-translate

我正在使用 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>

在这个 View 中,我有语言选择框。

这是我的 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.



任何帮助深表感谢。

最佳答案

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

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

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

关于angular - 找不到管道 'translate' 错误在 Ionic 4 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55000630/

相关文章:

Angular 2 服务错误。找不到原因

javascript - 如何在 Ionic/Angular 中访问 json 对象

cordova - 属性 'cordova'在 'Window'类型上不存在。 : ionic

angular - Nav和NavController的push方法有什么区别

progressive-web-apps - Ionic 4 - 如何配置 PWA 在子文件夹上运行?

angular - Ionic 4 - 如何使用 navCtrl 或 Router 服务在页面之间传递数据

node.js - 使用 Angular 和 Filesaver.js 下载文件不起作用

angular - 使用 ASYNC 管道和返回的 Observable<{}> 更新 UI

javascript - 如何在 angular 2 *ngFor 循环中使点击事件可选

angular - 如何使子页面的 ionic 标签可见?