0
我是新來Angular4(和打字稿)和我已經收到此錯誤:Angular 4 - 使用HttpModule我得到錯誤「沒有ConnectionBackend的提供者!」
我app.module.ts是設置這樣的:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { CustomerListComponent } from './Customer-List/customer-list.component';
import { CustomerDetailsComponent } from './Customer-Details/customer-details.component';
import { DataService } from './services/data.service';
@NgModule({
imports: [ BrowserModule, FormsModule, HttpModule ],
declarations: [ AppComponent, CustomerListComponent, CustomerDetailsComponent ],
providers: [ DataService ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
我有一個服務(稱爲DataService的)使用HTTP模塊下面的代碼:
import { Injectable } from '@angular/core';
import { Http, Response, RequestOptions, Headers } from '@angular/http';
import { CreateTestCustomers } from '../data/test-data';
import { Customer } from '../Models/customerModel';
@Injectable()
export class DataService {
private customersUrl = 'http://localhost:52740/api/Customers/';
constructor(
private http : Http
) { }
getCustomersP() : Promise<Customer[]>{
const customers = CreateTestCustomers();
return this.http.get(this.customersUrl)
.toPromise()
.then(response => {
const custs = response.json().data as Customer[];
return custs;
}, error => {
return Promise.reject('Something bad happened, please check the console');
},);
}
}
當我嘗試編譯我的應用程序,我得到這個奇怪的錯誤:
Unhandled Promise rejection: No provider for ConnectionBackend! ; Zone: <root> ; Task: Promise.then ; Value: Error: No provider for ConnectionBackend! Error: No provider for ConnectionBackend!
at ReflectiveInjector_.prototype._throwOrNull (eval code:2681:13)
at ReflectiveInjector_.prototype._getByKeyDefault (eval code:2720:13)
at ReflectiveInjector_.prototype._getByKey (eval code:2652:13)
at ReflectiveInjector_.prototype.get (eval code:2521:9)
at resolveNgModuleDep (eval code:9513:5)
at _createClass (eval code:9555:13)
at _createProviderInstance$1 (eval code:9524:13)
at resolveNgModuleDep (eval code:9508:13)
at _createClass (eval code:9555:13)
at _createProviderInstance$1 (eval code:9524:13)
zone.js (661,17)
"Unhandled Promise rejection:"
"No provider for ConnectionBackend!"
"; Zone:"
"<root>"
"; Task:"
"Promise.then"
"; Value:"
{
[functions]: ,
__proto__: { },
__zone_symbol__currentTask: { },
description: "No provider for ConnectionBackend!",
injectors: [ ],
keys: [ ],
message: "No provider for ConnectionBackend!",
name: "Error",
ngDebugContext: { },
ngOriginalError: undefined,
stack: "Error: No provider for ConnectionBackend!
at ReflectiveInjector_.prototype._throwOrNull (eval code:2681:13)
at ReflectiveInjector_.prototype._getByKeyDefault (eval code:2720:13)
at ReflectiveInjector_.prototype._getByKey (eval code:2652:13)
at ReflectiveInjector_.prototype.get (eval code:2521:9)
at resolveNgModuleDep (eval code:9513:5)
at _createClass (eval code:9555:13)
at _createProviderInstance$1 (eval code:9524:13)
at resolveNgModuleDep (eval code:9508:13)
at _createClass (eval code:9555:13)
at _createProviderInstance$1 (eval code:9524:13)"
}
"Error: No provider for ConnectionBackend!
at ReflectiveInjector_.prototype._throwOrNull (eval code:2681:13)
at ReflectiveInjector_.prototype._getByKeyDefault (eval code:2720:13)
at ReflectiveInjector_.prototype._getByKey (eval code:2652:13)
at ReflectiveInjector_.prototype.get (eval code:2521:9)
at resolveNgModuleDep (eval code:9513:5)
at _createClass (eval code:9555:13)
at _createProviderInstance$1 (eval code:9524:13)
at resolveNgModuleDep (eval code:9508:13)
at _createClass (eval code:9555:13)
at _createProviderInstance$1 (eval code:9524:13)"
有沒有人遇到過這個?一切都在網上似乎是指角2,並沒有修復它似乎幫助角4
我已經看到了這篇文章,但它沒有答案 - 我的問題類似於:提前angular 4 Error: Uncaught (in promise): Error: No provider for ConnectionBackend! while injecting Jsonp
謝謝!
顯示完整的代碼如何在DataService中導入http模塊 –
添加了完整的DataService代碼,謝謝! :) –
LoggerService可能也會有所幫助 – lexith