预期的声明或声明 javascript/typescript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35576307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:54:53  来源:igfitidea点击:

declaration or statement expected javascript/typescript

javascripttypescript

提问by XeniaSis

I am using Typescript 1.7 and React 0.14 with the new ES6 systax and I'm having the following destructuring assignment, as explained hereas well.

我使用的打字稿1.7和新ES6 systax阵营0.14,我有以下的解构赋值,如解释在这里为好。

let x0, x1, y0, y1;
if(this.props.viewport) {
    {x0, x1, y0, y1} = this.props.viewport;
}

However, I'm getting the Declaration or statement expectederror. What am I doing wrong?

但是,我收到Declaration or statement expected错误消息。我究竟做错了什么?

Thanks

谢谢

回答by XeniaSis

So, I found the problem. Had to wrap the whole line in parenthesis. So the following is correct.

所以,我发现了问题。必须将整行括在括号中。所以以下是正确的。

let x0, x1, y0, y1;
if(this.props.viewport) {
    ({x0, x1, y0, y1} = this.props.viewport);
}

回答by Worm

I had this problem because I was trying to use caseas a variable name e.g. var case = .... Now I know this error was because caseis already used by JavaScript's Switch Statement.

我遇到了这个问题,因为我试图将其case用作变量名,例如var case = .... 现在我知道这个错误是因为caseJavaScript 的Switch Statement已经使用了。