首页 文章

NullInjectorError:没有HubService的提供者

提问于
浏览
1

我开始学习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服务)时,我在浏览器中收到以下错误

ERROR错误:StaticInjectorError(AppModule)[SignUpComponent - > HubService]:StaticInjectorError(Platform:core)[SignUpComponent - > HubService]:NullInjectorError:没有HubService的提供者!在NullInjector.push ../ node_modules/@angular/core/fesm5/core.js.NullInjector.get(core.js:979)

1 回答

  • 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

相关问题