2017-07-28 51 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

這應該是被接受的答案。工作過,謝謝。 –

相關問題