2016-09-29 82 views
1

我正在從一個ES6項目(beta11)轉移到一個新的項目與rc0,我有一些問題,以重寫我的自定義http服務。Ionic 2 rc0:創建一個自定義http服務

在我的供應商/文件夾,我有customhttp.ts:

import {Injectable } from "@angular/core"; 
import {Http, Headers} from "@angular/http"; 
import {RequestOptions, XHRBackend} from '@angular/http'; 
@Injectable() 

export class CustomHttp extends Http { 
    constructor(xhrBackend: XHRBackend, requestOptions: RequestOptions) { 
    super(xhrBackend, requestOptions); 
    } 
    //some other methods, which override the defaults from Http 
} 
在我app.mmdule.ts

我有進口這一點,並在頁面我添加爲供應商

import {CustomHttp} from '../providers/customhttp'; 
... 

@NgModule({ 
    ... 
    providers: [CustomHttp] 
}) 

想要使用它

import {CustomHttp } from '../../providers/customhttp'; 
export class CheckAccess { 
    constructor(private customHttp: CustomHttp) 
} 

的.TS編譯但瀏覽器輸出

error_handler.js:45EXCEPTION:未捕獲(承諾中):錯誤:./CheckAccess類中的錯誤CheckAccess_Host - 內聯模板:0:0由於以下原因而導致:沒有爲ConnectionBackend提供程序!

我找不到我做錯了什麼。誰能幫忙?

+0

問題是帶班** ** ConnectionBackend。將此課程添加到提供者,您應該會很好! – Shmarkus

回答

1

導入XHRBackend和您的應用程序模塊中RequestOptions並通過以下設置爲供應商檢查:

providers :[ 
{ 
    provide: CustomHttp, 
    useFactory: (backend: XHRBackend, defaultOptions: RequestOptions) => { 
    return new CustomHttp(backend, defaultOptions); 
    }, 
    deps: [ XHRBackend, RequestOptions] 
    } 
] 
+0

嗨!我試過了,但問題仍然存在: - /仍然找不到提供ConnectionBackend –

+0

我有同樣的問題,這解決了它! – Gabriel