您好,这是我的事,我有一个消息服务,我想通过HTTP请求(Rest Api)从服务器收集数据,如下所示:
import { Sensor } from './sensor.model'
import { HttpClient } from '@angular/common/http';
export class MessageService{
mqttMessageData : JSON[]=[];
coapMessageData : JSON[]=[];
xmppMessageData : JSON[]=[];
constructor(private httpClient: HttpClient) {
}
getMqttMessages() {
this.httpClient.get<JSON>('http://127.0.0.1:5002/messages/mqtt').subscribe(data => {
this.mqttMessageData.push(data);
console.log(data);
});
}
但是我的浏览器控制台上出现此错误(ng serve中没有错误):
Uncaught Error: Can't resolve all parameters for MessageService: (?).
at syntaxError (compiler.js:1016)
at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getDependenciesMetadata (compiler.js:10917)
at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getTypeMetadata (compiler.js:10810)
at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getInjectableTypeMetadata (compiler.js:11032)
at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getProviderMetadata (compiler.js:11041)
at compiler.js:10979
at Array.forEach (<anonymous>)
at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getProvidersMetadata (compiler.js:10939)
at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNonNormalizedDirectiveMetadata (compiler.js:10416)
at CompileMetadataResolver.push../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getEntryComponentMetadata (compiler.js:11013)
所以我认为httpClient存在问题,但我不知道如何通过其他方式进行操作。
答案 0 :(得分:2)
您需要在服务上添加@Injectable
装饰器
import { Injectable } from '@angular/core'
@Injectable()
export class MessageService {
...
}