我在使用ngx-translate时遇到以下错误:
this.currentLoader.getTranslation(...).pipe is not a function
at TranslateService.getTranslation (core.es5.js:3171)
at TranslateService.retrieveTranslations (core.es5.js:3157)
at TranslateService.setDefaultLang (core.es5.js:3098)
at new AppComponent (app.component.ts:11)
at createClass (core.js:12470)
at createDirectiveInstance (core.js:12315)
at createViewNodes (core.js:13776)
at createRootView (core.js:13665)
at callWithDebugContext (core.js:15090)
at Object.debugCreateRootView [as createRootView] (core.js:14373)这是我的应用程序组件:
import { Component } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
@Component({
selector: 'app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(private translate: TranslateService) {
translate.setDefaultLang('en');
translate.use('en');
}
}在这里,我导入了模块,并在上面使用import声明了它
imports: [
TranslateModule.forRoot()
]我还导入了TranslateService:
providers: [
TranslateService
],如果我删除构造器中的行,我不会得到错误,但这意味着我也不会有翻译。我已经在以下位置创建了一个json文件: ClientApp/assets/i18n/en.json
我在Visual Studio提供的.net核心模板上运行它。我将模板从Angular 4升级到Angular 5。其他插件运行良好,我就是不能解决这个错误。
以下是版本:- "@ngx-translate/core":"9.1.1",- "@ngx-translate/http-loader":"2.0.1“- Angular 5.1.1
发布于 2018-06-12 17:51:59
您需要使用TranslateHttpLoader从"/assets/i18n/en.json“加载翻译
...
import {TranslateModule, TranslateLoader} from '@ngx-translate/core';
import {TranslateHttpLoader} from '@ngx-translate/http-loader';
...
// AoT requires an exported function for factories
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}
@NgModule({
imports: [
BrowserModule,
HttpClientModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
],
bootstrap: [AppComponent]
})
export class AppModule { }发布于 2018-06-23 12:17:47
它适用于@ngx-translate/core v8.0.0和9.1.1,我也遇到了同样的问题
package.json“@ngx-翻译/核心”:"8.0.0“
https://stackoverflow.com/questions/50813921
复制相似问题