问题:

在AppComponent private _dataService: DataService中添加(可视代码自动完成/添加)变量(constructor(…) { })时(因为我在下面使用了它),整个UI输出消失了。
从其中取出(或移至上方),一切都很好。

为什么?

项目是使用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
  ) { }

………





角CLI:1.7.4
节点:9.6.1
操作系统:Darwin x64
角度:5.2.9



编辑
刚意识到我忘记了Component的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!

最佳答案

首先,不建议使用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('...');
  }
}

关于javascript - AppComponent构造函数中的私有(private)变量->没有HTML/ View ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49800608/

10-11 13:25