问题:
当在AppComponent constructor(…) { }
中添加一个变量(/-a)时(因为我在下面使用它),整个UI输出就会消失。当从它中取出(或移动到上面)时,所有的都是精细。
为什么?
项目是用角CLI生成的(此时没有HTML自定义)。使用ng serve
查看输出。
import { Component } from '@angular/core';
import { DataService } from './services/DataService'
import { isType } from '@angular/core/src/type';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
constructor(
private _dataService: DataService
// private translate: TranslateService
) { }
………
角CLI: 1.7.4节点: 9.6.1 OS:达尔文x64角: 5.2.9
编辑刚刚意识到我忘记了组件的DataService (就像我也看到您已经评论过了) :P!
-->
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [DataService,Http]
})
但是问题仍然存在,…
控制台输出
Error: StaticInjectorError(AppModule)[Http -> ConnectionBackend]: StaticInjectorError(Platform: core)[Http -> ConnectionBackend]: NullInjectorError: No provider for ConnectionBackend!
发布于 2018-04-12 16:46:52
首先,不推荐使用HttpModule
,而是使用HttpClientModule
(参见https://angular.io/guide/http)。
然后,您不需要提供HttpClient
,而是需要import
HttpClientModule
在您的AppModule
中才能使用(注入) HttpClient
。
所以在你的模块里:
@NgModule({
imports: [HttpClientModule],
providers: [DataService]
})
export class AppModule { }
在你的服务中:
@Injectable()
export class DataService {
constructor(private http: HttpClient) { }
public doSomething() {
return this.http.get('...');
}
}
https://stackoverflow.com/questions/49800608
复制相似问题