使用Angular 2 AOT加载socket.io

时间:2017-01-23 04:23:13

标签: angularjs sockets angular2-aot rollupjs

我在angular2中使用socket.io创建了一个项目来发出/侦听套接字通信。在JIT(或npm start)中一切正常,但是当我尝试通过汇总编译代码将其作为AOT进行午餐时,生产使用它无法正常工作。

节点版本:6.9.4 npm版本:3.10.6

typings.json

.product-item .wrap-img img {
    image-rendering: auto;
    image-rendering: crisp-edges;
    image-rendering: pixelated;
}

systemjs.config.js

    {
  "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"
  }
}

package.json:     " socket.io-client":" ^ 1.7.2"

mycomponent.ts

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

" node_modules /的.bin / NGC" -p tsconfig-aot.json执行成功但是 " node_modules /的.bin /汇总" -c rollup-config.js给出错误:import * as io from 'socket.io-client'; var url = 'http://localhost:4500'; export var socket = io(url);

如果我更改了我的组件导入行并设置了cannot call a namespace ('io'),那么我会收到错误import io from socket.io-client

如果有人可以指导我,我将不胜感激,因为这是我第二天的故障排除和其他论坛的尝试。

先谢谢, 卡皮尔

1 个答案:

答案 0 :(得分: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: '' }
    })
},