【发布时间】:2018-12-09 06:19:53
【问题描述】:
我在 Angular6 中开发了一个项目。需要开发实时聊天部分。为此,我在我的 asp.net 核心 Web Apiproject 中使用 SignalR。现在我想在我的 Angular 项目中使用这个 Web Api 参考。
我正在使用this 链接。
但是在 App.Component.ts 中提供 Web Api url 时,我遇到了以下错误:
“HubConnection”类的构造函数是私有的,只能访问 在类声明中。
应用组件.ts:
import { HubConnection } from '@aspnet/signalr';
import { Message } from 'primeng/api';
export class AppComponent implements OnInit {
public _hubConnection: HubConnection;
msgs: Message[] = [];
constructor() {
}
ngOnInit() {
this._hubConnection = new HubConnection('http://localhost:1874/notify'); // getting error on this line.
编辑:尝试以下代码:-
修改 App.Component.ts :
import { HubConnection } from '@aspnet/signalr';
import * as signalR from '@aspnet/signalr';
import { Message } from 'primeng/api';
export class AppComponent implements OnInit {
public _hubConnection: HubConnection;
msgs: Message[] = [];
constructor() {
}
ngOnInit() {
this._hubConnection = new signalR.HubConnectionBuilder()
.withUrl('http://localhost:1874/notify')
.configureLogging(signalR.LogLevel.Information)
.build();
错误:
加载失败 http://localhost:1874/notify/negotiate:对预检请求的响应 未通过访问控制检查: 响应中的“Access-Control-Allow-Credentials”标头是“” 当请求的凭据模式为“包含”时,必须为“真”。 因此,Origin 'http://localhost:4200' 不允许访问。这 XMLHttpRequest 发起的请求的凭证模式是 由 withCredentials 属性控制。
【问题讨论】:
标签: signalr signalr-hub angular6 angular-cli-v6