typescript 预期的参数声明(打字稿)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41836897/
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
Parameter declaration expected (typescript)
提问by lowcrawler
@Vinay in this TypeScript + AngularJS 1: How to connect enum with select directive?question shows a relatively simple way to get a array for building a select drop-down in angular.
@Vinay 在这个TypeScript + AngularJS 1:如何将 enum 与 select 指令连接?问题显示了一种相对简单的方法来获取用于构建角度选择下拉列表的数组。
Unfortunately, I try to ape this code and I get errors ... first upon declaring the 'colors' array if I use var or let... (but it works if I don't). Unfortunately, that just moves the error to the next variable declaration in the setup of the for loop. Unfortunately, here, I can't not put in a let
or a var
.
不幸的是,我试图模仿这段代码,但我得到了错误……首先,如果我使用 var 或 let 声明“颜色”数组……(但如果我不这样做,它会起作用)。不幸的是,这只是将错误移至 for 循环设置中的下一个变量声明。不幸的是,在这里,我不能不放入 alet
或 a var
。
I'm sure this is simple, but I'm just banging me head and missing it.
我确定这很简单,但我只是在敲我的头并想念它。
enum Color {
Green = <any>"Green",
Red = <any>"Red",
Blue = <any>"Blue"
}
export class ClassName {
colors: string[] = []; // <-- get error here if I declare var or let
for (var item in Color) { // <-- get error here
if (Color.hasOwnProperty(item)) {
this.colors.push(item);
}
}
}
采纳答案by Ryan Cavanaugh
Property declarations belong in the body, but executable code goes in the constructor:
属性声明属于主体,但可执行代码在构造函数中:
export class ClassName {
colors: string[] = []; // <-- get error here if I declare var or let
constructor() {
for (var item in Color) { // <-- get error here
if (Color.hasOwnProperty(item)) {
this.colors.push(item);
}
}
}
}