在新项目中升级ionic2后(从头开始).. 不能使用 socket.io 。
import * as io from 'socket.io-client'; ... this.socket = io(url); ....
构建失败: bundle prod失败:无法调用命名空间('io')
在以前的版本中,它通过以下方式解决:
typings install dt~socket.io-client --save --global
错误打印和配置如下:
[12:19:27] bundle prod failed: Cannot call a namespace ('io')
[12:19:27] Error: Cannot call a namespace ('io')
at error (/*****************/node_modules/rollup/dist/rollup.js:5404:12)
at CallExpression.bind (/*****************/node_modules/rollup/dist/rollup.js:5837:5)
at /*****************/node_modules/rollup/dist/rollup.js:5149:50
at AssignmentExpression.eachChild (/*****************/node_modules/rollup/dist/rollup.js:5166:5)
at AssignmentExpression.bind (/*****************/node_modules/rollup/dist/rollup.js:5149:7)
at AssignmentExpression.bind (/*****************/node_modules/rollup/dist/rollup.js:5509:23)
at /*****************/node_modules/rollup/dist/rollup.js:5149:50
at ExpressionStatement.eachChild (/*****************/node_modules/rollup/dist/rollup.js:5166:5)
at ExpressionStatement.bind (/*****************/node_modules/rollup/dist/rollup.js:5149:7)
at BlockStatement.bind (/*****************/node_modules/rollup/dist/rollup.js:5615:9)
Error running ionic app script "build": Error: Cannot call a namespace ('io')
npm ERR! Linux 4.4.0-38-generic
npm ERR! argv "/.nvm/versions/node/v6.7.0/bin/node" "/.nvm/versions/node/v6.7.0/lib/node_modules/npm/bin/npm-cli.js" "run-script" "build"
npm ERR! node v6.7.0
npm ERR! npm v3.10.3
npm ERR! code ELIFECYCLE
npm ERR! ionic-hello-world@ build: `ionic-app-scripts build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the ionic-hello-world@ build script 'ionic-app-scripts build'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the ionic-hello-world package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! ionic-app-scripts build
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs ionic-hello-world
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls ionic-hello-world
npm ERR! There is likely additional logging output above.
...
的package.json
{
...
"dependencies": {
"@ionic/storage": "^1.0.3",
"ionic-angular": "^2.0.0-rc.0",
"ionic-native": "^2.0.3",
"ionicons": "^3.0.0",
"socket.io-client": "^1.5.0"
},
"devDependencies": {
"@ionic/app-scripts": "latest",
"typescript": "^2.0.3"
},
...
"cordovaPlugins": [
"cordova-plugin-device",
"cordova-plugin-console",
"cordova-plugin-whitelist",
"cordova-plugin-splashscreen",
"cordova-plugin-statusbar",
"ionic-plugin-keyboard"
],
"cordovaPlatforms": []
}
typings.json
{
"globalDependencies": {
"socket.io-client": "registry:dt/socket.io-client#1.4.4+20160317120654"
}
}
答案 0 :(得分:2)
语法
import io from 'socket.io-client'
..不标准,您将遇到运行时错误:
保持语法import * as io from 'socket.io-client'
,然后在Typescript中使用任意调用默认方法:
var ioFunc = (io as any).default ? (io as any).default : io;
this.socket = ioFunc(url);
答案 1 :(得分:0)
解决方案:没有通配符导入...
从'socket.io-client'导入io;
而不是
从'socket.io-client'
导入*作为io