更新时间:2023-02-16 23:19:13
在新版本的 angular2 CLI https://cli.angular.io/ 添加库很简单.
In new version of angular2 CLI https://cli.angular.io/ it is simple to add library.
如果你只想添加 sockjs_client
If you want to add only sockjs_client
1) npm i --save sockjs-client
2) 在 typings.d.ts 中添加这个声明 declare module 'sockjs-client';
2) In typings.d.ts add this declaration declare module 'sockjs-client';
但我建议为 angular 2 使用 STOMP-Over-WebSocket 服务
But I would recommend to use STOMP-Over-WebSocket Service for angular 2
安装这个 npm 包
npm i --save stompjs
npm i --save sockjs-client
npm i --save ng2-stomp-service
在typings.d.ts
添加stompjs
和sockjs-client
模块声明
declare module 'stompjs';
declare module 'sockjs-client';
在app.module.ts
import { StompService } from 'ng2-stomp-service';
@NgModule({
...
providers: [StompService]
})
在app.components.ts
import { StompService } from 'ng2-stomp-service';
private wsConf = {
host:'test.com'
}
constructor(stomp: StompService) {
stomp.configure(this.wsConf);
stomp.startConnect().then(() => {
console.log('connected');
});
}