AppComponent 构造函数中的私有变量 -> 否 HTML/View?
Private variable in AppComponent constructor -> NO HTML/View?
问题:
当在 AppComponent constructor(…) { }
中添加 (Visual Code autocompleted/-added) 一个变量 (private _dataService: DataService
) 时(因为我在下面使用了它)不知何故整个 UI输出消失。
当取出它(或移到上面)时,一切都是 fine。
为什么?
项目是用 Angular-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
) { }
………
Angular 命令行界面:1.7.4
节点:9.6.1
OS:达尔文 x64
Angular: 5.2.9
编辑
刚刚意识到我忘记了组件的数据服务(就像我也看到你已经评论过一样):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!
首先,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('...');
}
}
问题:
当在 AppComponent constructor(…) { }
中添加 (Visual Code autocompleted/-added) 一个变量 (private _dataService: DataService
) 时(因为我在下面使用了它)不知何故整个 UI输出消失。
当取出它(或移到上面)时,一切都是 fine。
为什么?
项目是用 Angular-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
) { }
………
Angular 命令行界面:1.7.4 节点:9.6.1 OS:达尔文 x64 Angular: 5.2.9
编辑 刚刚意识到我忘记了组件的数据服务(就像我也看到你已经评论过一样):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!
首先,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('...');
}
}