【问题标题】:NullInjectorError: No provider for HubServiceNullInjectorError:没有 HubService 的提供者
【发布时间】: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.jsonangular.json

标签: angular signalr angular-services


【解决方案1】:

在 AppModule 中

 import { HubService } from 'ngx-signalr-hubservice'; 

在 UserConnectionServiceService 中:

import {
  HubService,
  Hub,
  HubSubscription,
  HubWrapper
} from 'ngx-signalR-hubservice';

应该是ngx-signalr-hubservice

我认为这是作者的错字。这是 npm 链接:https://www.npmjs.com/package/ngx-signalr-hubservice

【讨论】:

  • @Yedidyakfir 没问题。我认为这是作者的错字,请查看我发布的链接。
猜你喜欢
  • 2019-11-16
  • 2021-01-20
  • 2019-06-25
  • 2018-09-19
  • 2018-05-02
  • 2020-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多