我正在尝试在该库中创建 Angular 库;我创建了一个单例类来处理相同的 SignalR 连接,这是我的代码:
import * as signalR from '@microsoft/signalr';
export class SignalRConnection {
static _instance: SignalRConnection;
connection: signalR.HubConnection;
private constructor() {
this.connection = new signalR.HubConnectionBuilder()
.withUrl("http://localhost:5000/hub")
.build();
// TODO: something on reject
this.connection.start().catch(console.log);
}
/**
* Get exists or create new instance of SignalR connection
*/
static getOrCreate(): SignalRConnection {
if (SignalRConnection._instance == undefined) {
SignalRConnection._instance = new SignalRConnection();
}
return SignalRConnection._instance;
}
}
此代码在开发过程中按预期方式工作(我使用public-api
使用它),但是当我尝试构建程序包时,出现此错误:
error encountered
in metadata generated for exported symbol 'SignalRConnection':
Metadata collected contains an error that will be reported at runtime: Only initialized variables and constants can be referenced because the value
of this variable is needed by the template compiler.
编译时发生了什么变化,我该如何解决?
答案 0 :(得分:0)
export class SignalRConnection implements OnInit {
static _instance: any = null;
ngOnInit() {
SignalRConnection._instance= this;
}
}