2017-01-23 108 views
0

我已經使用socket.io在angular2中創建了一個項目來發射/偵聽套接字通信。在JIT(或npm開始)中一切正常,但是當我嘗試通過彙總將代碼編譯爲AOT進行生產使用時,它不起作用。使用Angular 2 AOT加載socket.io

節點版本:6.9.4 NPM版本:3.10.6

typings.json

{ 
    "globalDependencies": { 
    "core-js": "registry:dt/core-js#0.0.0+20160725163759", 
    "jasmine": "registry:dt/jasmine#2.2.0+20160621224255", 
    "node": "registry:dt/node#7.0.0+20170110233017", 
    "socket.io-client": "registry:dt/socket.io-client#1.4.4+20161116080703" 
    }, 
    "ambientDependencies": { 
    "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#138ad74b9e8e6c08af7633964962835add4c91e2", 
    "socket-io-client": "github:DefinitelyTyped/DefinitelyTyped/socket.io-client/socket.io-client.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd", 
    "es6-shim": "registry:dt/es6-shim#0.31.2+20160317120654" 
    } 
} 

systemjs.config.js

map : {"socket.io-client": 'npm:socket.io-client'} 
packages : { "socket.io-client": { 
      main: './socket.io.js', 
      "defaultExtension": "js" 
     } } 

的package.json: 「插座.io-client「:」^ 1.7.2「

mycomponent.ts

import * as io from 'socket.io-client'; 
var url = 'http://localhost:4500'; 
export var socket = io(url); 

「node_modules /的.bin/NGC」 -p tsconfig-aot.json成功執行,但 「node_modules /的.bin /彙總」 -c彙總-config.js給出了錯誤:cannot call a namespace ('io')

如果我改變我的組件進口線,並設置import io from socket.io-client然後我得到錯誤module 'socket.io-client' has no default export

如果有人可以指導我通過,因爲這是我的第二天的疑難解答和嘗試按其他論壇,我將不勝感激。

由於事先 卡皮爾

回答

0

保持進口:

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

並添加插件rollup.js:

{ 
    name: 'replace io imports', 
    transform: code => ({ 
     code: code.replace(/import\s*\*\s*as\s*io/g, 'import io'), 
     map: { mappings: '' } 
    }) 
},