首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AppComponent构造函数中的私有变量->没有->/视图?

AppComponent构造函数中的私有变量->没有->/视图?
EN

Stack Overflow用户
提问于 2018-04-12 15:35:04
回答 1查看 187关注 0票数 1

问题:

当在AppComponent constructor(…) { }中添加一个变量(/-a)时(因为我在下面使用它),整个UI输出就会消失。当从它中取出(或移动到上面)时,所有的都是精细

为什么?

项目是用角CLI生成的(此时没有HTML自定义)。使用ng serve查看输出。

代码语言:javascript
运行
复制
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!

-->

代码语言:javascript
运行
复制
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [DataService,Http]
})

但是问题仍然存在,…

控制台输出

代码语言:javascript
运行
复制
Error: StaticInjectorError(AppModule)[Http -> ConnectionBackend]: StaticInjectorError(Platform: core)[Http -> ConnectionBackend]: NullInjectorError: No provider for ConnectionBackend!
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-12 16:46:52

首先,不推荐使用HttpModule,而是使用HttpClientModule (参见https://angular.io/guide/http)。

然后,您不需要提供HttpClient,而是需要import HttpClientModule在您的AppModule中才能使用(注入) HttpClient

所以在你的模块里:

代码语言:javascript
运行
复制
@NgModule({
  imports: [HttpClientModule],
  providers: [DataService]
})
export class AppModule { }

在你的服务中:

代码语言:javascript
运行
复制
@Injectable()
export class DataService {

  constructor(private http: HttpClient) { }

  public doSomething() {
    return this.http.get('...');
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49800608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档