【发布时间】:2018-05-30 19:12:34
【问题描述】:
我开始学习 signalr 和 angular,我想创建一个可以使用 signalr 与浏览器对话的应用。 我在角度使用 ngx-signalR-hubservice 与信号器应用程序进行通信。我创建了一个简单的服务来测试代码
import { Injectable } from '@angular/core';
import {
HubService,
Hub,
HubSubscription,
HubWrapper
} from 'ngx-signalR-hubservice';
@Injectable({providedIn: 'root'})
@Hub({hubName: 'DALHub'})
export class UserConnectionServiceService {
private hubWrapper: HubWrapper;
constructor(private hub: HubService) {
this.hubWrapper = this.hub.register(this);
this.hub.connect({url: 'http://localhost:10476/signalr'});
}
async SaveKey() {
await this.hubWrapper.invoke('SaveKey', 'yedidya', 'kfiry');
console.log('awesome');
}
@HubSubscription()
OK() {
console.log('GOT IT');
}
}
这是我的应用组件
import { NgModule } from '@angular/core';
import { HubService } from 'ngx-signalr-hubservice';
@NgModule({
declarations: [
AppComponent,
SignUpComponent,
LogInComponent
],
imports: [
],
providers: [HubService],
bootstrap: [AppComponent]
})
export class AppModule {}
这是我使用我创建的服务的代码
import { Component, OnInit } from '@angular/core';
import {UserConnectionServiceService} from '../../Services/UserConnectionService/user-connection-service.service';
@Component({
selector: 'app-sign-up',
templateUrl: './sign-up.component.html',
styleUrls: ['./sign-up.component.css']
})
export class SignUpComponent implements OnInit {
public save(): void {
this.hub.SaveKey();
}
constructor(private hub: UserConnectionServiceService) { }
ngOnInit() {
}
}
但是当我尝试运行它时(我使用 ng serve),我在浏览器中收到以下错误
错误错误:StaticInjectorError(AppModule)[SignUpComponent -> HubService]:StaticInjectorError(平台:核心)[SignUpComponent -> HubService]:NullInjectorError:没有 HubService 的提供者!在 NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:979)
【问题讨论】:
-
也可以尝试将其添加到模块
imports。 -
您是否也将
UserConnectionServiceService添加到提供程序并将导入语句HubService 添加到UserConnectionServiceService中? -
分享您的
angular.cli.json或angular.json
标签: angular signalr angular-services