0

我想做一個套接字處理程序。所以在我的角色2代碼中,我得到了一個名爲「socket.service.ts」的服務。加載角度2服務內的動態服務

此服務導入套接字io lib並創建套接字。

但我試圖從json文件中獲取不同的處理程序。 例如: 用戶在config.json文件中編輯「處理程序」鍵,它應該更改套接字的處理程序。

我想這個處理程序有一個角2服務語法。所以我試圖導入這個動態服務與

System.import(path).then() 

但它給了我一個錯誤。

這裏是我的socket.service:

private _byHandler() { 
    var _this = this; 

    this._initHandler((socketHandler) => { 
    _this.config = socketHandler.getConfig(_this.config); 
    _this._handler = socketHandler; 
    _this._createSocket(); 
    }); 
} 

private _initHandler(callback) { 
    let _this = this, 
     handler = this.config.handler, 
     path = "./../handlers/socket/" + handler + "/" + handler + "handler"; 

    System.import(path).then(callback); 
} 

謝謝!

回答

0

好的我找到了解決方案: 首先我的路徑變量不好,我們應該知道System.import返回的值不是類本身,而是一個包含類的對象。

的。那麼(

左右)應該這樣做:

let handlerClass = null; 
    for(let k in socketHandler) { 
    handlerClass = k; //Get the first key of the object cause we don't know the class name 
    break; 
    } 

    _this._handler = new socketHandler[handlerClass]();