gpt4 book ai didi

angularjs - typescript :TS2496: 'arguments' 对象不能在 ES3 和 ES5 的箭头函数中引用。考虑使用标准函数表达式

转载 作者:搜寻专家 更新时间:2023-10-30 20:35:26 25 4
gpt4 key购买 nike

我遵循了一些教程,例如 http://www.html5rocks.com/en/tutorials/frameworks/angular-websockets/在我的 node.js 服务器和 angular.js 客户端中实现 socket.io,一切正常。

但是,客户端,Typescript 显示以下编译错误:

TS2496: The 'arguments' object cannot be referenced in an arrow function in ES3 and ES5. Consider using a standard function expression.

关于以下服务代码:

on(eventName:string, callback) {
this.socket.on(eventName, () => {
var args = arguments; // <-- here the compilation error
this.$rootScope.$apply(() => {
callback.apply(this.socket, args);
});
});
}

emit(eventName:string, data, callback?) {
this.socket.emit(eventName, data, () => {
var args = arguments; // <-- here the compilation error
this.$rootScope.$apply(() => {
if (callback) {
callback.apply(this.socket, args);
}
});
});
}

我尝试了不同的解决方案,例如 https://github.com/Microsoft/TypeScript/issues/1609 的第一篇文章中描述的解决方案,它修复了编译但破坏了 socket.io 的“开启”功能(不再解释 json 对象)。

知道如何修复或忽略此 typescript 编译错误吗?

最佳答案

另一种可能性是使用 rest parameter而不是参数:

on(eventName:string, callback) {
this.socket.on(eventName, (...args) => {
this.$rootScope.$apply(() => {
callback.apply(this.socket, args);
});
});
}

关于angularjs - typescript :TS2496: 'arguments' 对象不能在 ES3 和 ES5 的箭头函数中引用。考虑使用标准函数表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38077729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com