Rag*_*hav 7 ionic-framework ngx-translate angular ionic4
我正在使用 Ionic 4 应用程序并安装了该ngx-translate插件。它工作正常,app.component.html但tabs.page.html显示错误。
找不到管道“翻译”
这是我的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>
Run Code Online (Sandbox Code Playgroud)
在这个视图中,我有语言选择框。
这是我的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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的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]
}
}) ],
})
Run Code Online (Sandbox Code Playgroud)
在 中app.component.html,它工作正常,但在 中tabs.pahe.html不起作用。
这是在tabs.page.html:
<ion-label>{{ 'ACCOUNT_TAB_LAB' | translate }}</ion-label>
Run Code Online (Sandbox Code Playgroud)
错误:找不到管道“翻译”。
任何帮助深表感谢。
Bun*_*ner 12
您需要TranslateModule在要使用translate管道的每个模块中导入。
import { TranslateModule } from '@ngx-translate/core';
...
imports: [
TranslateModule // do not call forRoot from any module other than AppModule
]
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6277 次 |
| 最近记录: |