2017-07-28 54 views
6

我正在开发一个Ionic 2移动应用程序,并且想要使用ngx-translate功能。 在介绍之后,我输入必要的文件在应用模块是这样的:类型'Http'的参数不能分配给离子类型'Http'的参数ngx-translate

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

export function createTranslateLoader(http: Http) { 
    return new TranslateHttpLoader(http, './assets/i18n/', '.json'); 
} 

这给错误:

Argument of type 'Http' is not assignable to parameter of type 'Http'. 
Property 'handler' is missing in type 'Http' 

我认为这是由预计的程序包不匹配NGX,但翻译我无法弄清楚什么和如何。我的@ angular/http版本是4.3.2 有没有人知道该怎么做?

回答

18

问题是由于冲突的版本,也许你安装一个“^ 1.0.2”版本“@ NGX-翻译/ HTTP装载机” althought你的函数可用为以前的版本。别担心!你刚才使用的HttpClient而非http ..

不要忘记改变“DEPS”不变的价值和你的模块中导入HttpClientModule(或您的app.module)

don't forget to change the value of 'deps' constant and import the HttpClientModule in your module (or in your app.module)

14

尝试使用HttpClient的

import {HttpClientModule, HttpClient} from '@angular/common/http'; 
import {TranslateModule, TranslateLoader} from '@ngx-translate/core'; 
import {TranslateHttpLoader} from '@ngx-translate/http-loader'; 
import {AppComponent} from "./app.component"; 

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

@NgModule({ 
    declarations: [ 
     AppComponent 
     ], 
    imports: [ 
     BrowserModule, 
     HttpClientModule, 
     TranslateModule.forRoot({ 
      loader: { 
       provide: TranslateLoader, 
       useFactory: HttpLoaderFactory, 
       deps: [HttpClient] 
      } 
     }) 
    ], 
    bootstrap: [AppComponent] 
}) 
export class AppModule { } 
+0

你的建议为我工作。谢谢!!! –

+0

像魅力一样工作,应该被接受为答案。 –

+0

这应该是被接受的答案。工作过,谢谢。 –

相关问题