Angular2/http异常无连接后端
我试图使用angular2 http请求,但出现以下错误: [错误]异常:ConnectionBackend没有提供程序!(AppComponent->Http->ConnectionBackend) 我已经安装了一个裸体应用程序,它也有同样的问题,我不知道是什么导致了它 提前谢谢 app.component.tsAngular2/http异常无连接后端,http,service,angular,Http,Service,Angular,我试图使用angular2 http请求,但出现以下错误: [错误]异常:ConnectionBackend没有提供程序!(AppComponent->Http->ConnectionBackend) 我已经安装了一个裸体应用程序,它也有同样的问题,我不知道是什么导致了它 提前谢谢 app.component.ts import {Component} from 'angular2/core'; import {Http, Headers} from 'angular2/http'; @Com
import {Component} from 'angular2/core';
import {Http, Headers} from 'angular2/http';
@Component({
selector: 'app',
template: `
<button (click)="getEmployee()">Get</button>
<p>{{employee.email}}</p>
`,
providers: [Http]
})
export class AppComponent {
employee: {"email": "bob"};
http: Http;
constructor(http:Http){
this.http = http;
}
getEmployee(){
this.http.get('localhost:8080/employee-rates?id=0')
.map(response => response.json()).subscribe(result => this.employee = result);
}
}
index.html
<html>
<head>
<base href="/">
<title>Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="js/es6-shim/es6-shim.min.js"></script>
<script src="js/systemjs/dist/system-polyfills.js"></script>
<script src="js/angular2/bundles/angular2-polyfills.js"></script>
<script src="js/systemjs/dist/system.src.js"></script>
<script src="js/rxjs/bundles/Rx.js"></script>
<script src="js/angular2/bundles/angular2.dev.js"></script>
<script src="js/angular2/bundles/http.dev.js"></script>
<!-- 2. Configure SystemJS -->
<script>
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import('app/main')
.then(null, console.error.bind(console));
</script>
</head>
<!-- 3. Display the application -->
<body>
<div class='small-12'>
<app>Loading...</app>
</div>
</body>
</html>
试验
System.config({
套餐:{
应用程序:{
格式:'寄存器',
defaultExtension:'js'
}
}
});
System.import('app/main')
.then(null,console.error.bind(console));
加载。。。
从代码中删除HTTP\u绑定。它们是不推荐使用的,并且引用了HTTP\u提供程序
,所以就好像您使用了两次提供程序一样
bootstrap(AppComponent, [HTTP_PROVIDERS]);
此外,您不需要在组件中使用提供程序:[Http]
,因为您在引导中注入了Http\u提供程序。将其添加到组件的提供程序将创建服务的新实例
@Component({
providers: []
})
src/main.ts
import { HTTP_PROVIDERS, ConnectionBackend } from '@angular/http';
bootstrap(AppComponent, [
HTTP_PROVIDERS,
ConnectionBackend
]);
使用角度-cli@1.0.0-beta.10和angular@2.0.0-rc.4从Angular 2 RC5版本开始,在NgModule文件中添加:
import { HttpModule } from '@angular/http';
@NgModule({
imports: [
...
HttpModule ]
刚刚测试了这个,不幸的是问题仍然存在。没有问题。欢迎来到SOHTTP\U PROVIDERS
不再是一个东西为什么导入而不提供?@Markus,因为HttpModule
是一个模块(而不是服务),必须导入。Yap。就是这样。我想出来了。在最终的产品发布中有什么想法吗?[这里是一个新的答案]
import { HttpModule } from '@angular/http';
@NgModule({
imports: [
...
HttpModule ]