angular – 类“HubConnection”的构造方法是私有的,只能在类声明中访问

我正在尝试实现角度和信号器项目.我从 Medium获得样本

我已经安装了所有必需的软件包,并在app.component.ts中输入以下代码:

import { Component,OnInit } from '@angular/core';
import { HubConnection } from '@aspnet/signalr';

import { Message } from 'primeng/api';

@Component({
  selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'PROJECT TITLE';
  base_url = 'http://localhost:8081/';

  hubConnecton: HubConnection;
  msgs: Message[] = [];

  constructor() { 

  }

  ngOnInit(): void {
    this.hubConnecton = new HubConnection(this.base_url);
  }
}

但是我在= new HubConnection(this.base_url)中收到错误;使用此消息:类“HubConnection”的构造方法是私有的,只能在类声明中访问.

enter image description here

我需要你的帮助.谢谢.

解决方法

您必须使用HubConnectionBuilder().

import { Component,OnInit } from '@angular/core';
import { HubConnection,HubConnectionBuilder } from '@aspnet/signalr';
import { Message } from 'primeng/api';

@Component({
  selector: 'app-root',styleUrls: ['./app.component.css']
})
export class AppComponent {

  public _hubConnecton: HubConnection;
  msgs: Message[] = [];

  constructor() { }

  ngOnInit(): void {
    this._hubConnecton = new HubConnectionBuilder().withUrl("http:/:localhost:1874/notify").build();
    this._hubConnecton
      .start()
      .then(() => console.log('Connection started!'))
      .catch(err => console.log('Error while establishing connection :('));

    this._hubConnecton.on('BroadcastMessage',(type: string,payload: string) => {
      this.msgs.push({ severity: type,summary: payload });
    });
  }
}

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...