[英]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.