简体   繁体   English

在Angular 2应用程序中将ngx-toastr与socket.io一起使用

[英]Using ngx-toastr with socket.io in Angular 2 application

I have an Angular 2 application with socket.io. 我有一个与socket.io的Angular 2应用程序。 Here's how the socket.io.ts file looks like: 这是socket.io.ts文件的样子:

import * as defSocketIO from 'socket.io-client';

export class SocketIO {

public static bootstrap(options : object) : SocketIO {
    return new SocketIO(options);
}

constructor(options: Object) {

    (<any> window).SocketIO = new defSocketIO(
        location.protocol + '//' + location.hostname + (location.port == '' ? '' : ':' + location.port),
        {
            'reconnection'          : false,
            'reconnectionAttempts'  : 0,
            'reconnectionDelay'     : 100000,
            'timeout'               : 900000
        }
    );

    (<any> window).SocketIO.on('connect',  function () {
            console.log("Connected to Socket.io server...");
        }
    );

    (<any> window).SocketIO.on('disconnect',  function () {
            console.log("Disconnected to Socket.io server...");
        }
    );

However in the application I'm using ngx-toastr , and It's working fine in components, but I would like to use it for socket messages. 但是,在应用程序中,我使用的是ngx-toastr ,并且在组件中运行良好,但是我想将其用于套接字消息。 I've tried to import it several ways, but I can't get it work. 我尝试了几种导入方式,但是无法正常工作。 Am I able to inject it somehow? 我能以某种方式注入吗? Here is ngx-toastr 是ngx-toastr

Thanks! 谢谢!

最后,我通过从Toastr创建自己的服务并将其导入socket.io.ts文件中解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM