[英]Angular 4 - using HttpModule I get the error “No provider for ConnectionBackend!”
我是Angular4(和TypeScript)的新手,並且遇到了以下錯誤:
我的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 { }
我有一個在以下代碼中使用Http模塊的服務(稱為DataService):
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)"
有人遇到過嗎? 在線上的所有內容似乎都涉及Angular 2,而對其進行的所有修復都似乎對Angular 4沒有幫助。
我看過這篇文章,但未得到解答-我的問題看起來很相似: angular 4錯誤:未捕獲(承諾):錯誤:沒有ConnectionBackend的提供程序! 在注入Jsonp時
提前致謝!
與其說是答案,不如說是一種解決方法。 我搭建了一個新項目,並逐個添加了我的組件以解決此問題。 不知道原始項目發生了什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.