angular - Angular向构造函数添加服务导致没有内容显示
问题描述
每当我尝试在该组件的某些组件的构造函数内容中添加服务时,都不会显示。
我的简单服务如下所示:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { News } from 'src/app/interfaces/news';
@Injectable(
{providedIn: 'root'}
)
export class NewsService {
public newsUrl = 'some url';
constructor(private http: HttpClient) { }
public getNews(): Observable<News[]> {
console.log(this.newsUrl);
return this.http.get<News[]>(this.newsUrl+'/news/all');
}
}
我什至没有使用它,只是像这样传入构造函数:
导出类 TestComponent ...
构造函数(私人新闻服务:新闻服务){}
解决方案
确保你在你的模块中导入:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [
BrowserModule,
// import HttpClientModule after BrowserModule.
HttpClientModule,
],
declarations: [
AppComponent,
],
bootstrap: [ AppComponent ]
})
export class AppModule {}
源文档
推荐阅读
- java - 如何通过 http 使用 java 检查 loadrunner 事务状态?
- c# - System.Collections.Generic.List<(float[],float[])> 的平均值
- sql - 日期字段上的表达式
- python - TFX 组件 CsvExampleGen 总是产生带有空输出(和输入)的示例
- javascript - React / Redux使用状态更改嵌套数组中按钮的背景颜色
- php - 出现引导警报消息时内容流动
- css - 无法使用 Bootstrap 实现我自己的样式表?
- math - 给定其他几个百分位数,我如何估计一个值的百分位数?
- sql - 如何限制sql中每个值一个结果?
- c# - 平面视图模型不正确的输出