gpt4 book ai didi

angular - 类 'HubConnection' 的构造函数是私有(private)的,只能在类声明中访问

转载 作者:太空狗 更新时间:2023-10-29 17:16:59 24 4
gpt4 key购买 nike

我正在尝试实现一个 Angular 和信号器项目。我从 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); 中收到一条错误消息:Constructor of class 'HubConnection' is private and only accessible within the class declaration.

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',
templateUrl: './app.component.html',
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 - 类 'HubConnection' 的构造函数是私有(private)的,只能在类声明中访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51852077/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com