我有一个组件使用了translateService,但是在组件模板HTML中使用管道无法翻译项目,会出现以下错误:
找不到管道“translate”
app.module.ts
import {BrowserModule} from "@angular/platform-browser";
import {NgModule} from "@angular/core";
import {HttpModule, Http} from "@angular/http";
import {TranslateModule, TranslateLoader, TranslateStaticLoader} from 'ng2-translate';
import {AppComponent} from "./app.component";
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
HttpModule,
TranslateModule.forRoot({
provide: TranslateLoader,
useFactory: (http: Http) => new TranslateStaticLoader(http, './assets/i18n', '.json'),
deps: [Http]
})
],
bootstrap: [AppComponent]
})
export class AppModule {
}
预订.component.ts
import {Component, OnInit} from '@angular/core';
import {BookingComponent} from './booking.component';
import {TranslateService} from 'ng2-translate';
@Component({
selector: 'app-booking',
templateUrl: './booking.component.html',
styleUrls: ['./booking.component.css']
})
export class BookingComponent implements OnInit {
constructor(private translate: TranslateService
) {
translate.setDefaultLang('de');
translate.use('de');
};
ngOnInit() {
}
}
booking.component.html
<p>{{'TESTKEY' | translate }}</p>
使用组件服务的翻译很好,但我还需要使用管道来翻译HTML。