我有一个简单的React组件来更新一些东西。
当我试图运行它时,我得到了这个错误:
'arguments'只允许在函数和类方法中使用。
错误在updateConnection
函数的组件中:
import { updateConnection } from '../connections';
export default class Connection extends React.PureComponent {
updateConnection = (name, value, save = true) => {
if (arguments.length === 0) {
updateConnection(this.state.connection);
return;
}
导入的updateConnection
只是一个简单的API调用:
export function updateConnection(connection) {
return api.put(`/api/connections/${connection.id}`, connection);
}
但updateConnection
是一个函数…为什么会出现这个错误呢?
完全错误:
SyntaxError: G:ConnectMeConnectClientConnectorcomponentsConnection.js: 'arguments' is only allowed in functions and class methods. (124:12)
看起来您正在尝试在箭头函数中使用arguments
关键字。关键字arguments
不能在箭头函数中使用
const myFunction = () => {
// cannot use arguments in an arrow function
console.log(arguments);
}
要在箭头函数中使用参数,可以使用展开运算符
const myFunction = (...args) => {
console.log(args); // args contains function's arguments as an Array
}
但是,在函数声明中,可以使用arguments
关键字。
function myFuction () {
console.log(arguments); // can be used here
}
或者,您可以在类方法中使用
class MyClass {
classMethod () {
console.log(arguments); // can be used here
}
}