Angular 缺少类型脚本重复标识符

Angular 缺少类型脚本重复标识符,angular,typescript,Angular,Typescript,我试图在我正在开发的一个有角度的网页中的一个TypeScript程序中做一些if/else语句。这是一个可复制的案例: export class AppComponent { x: number = 0; output: number = 0; if (this.x < 0.5){ this.output = 1; } else if ((this.x >= 0.5) && (this.x < 1.0)){ this.out

我试图在我正在开发的一个有角度的网页中的一个TypeScript程序中做一些if/else语句。这是一个可复制的案例:

export class AppComponent {
  x: number = 0;
  output: number = 0;

  if (this.x < 0.5){
    this.output = 1;
  }
  else if ((this.x >= 0.5) && (this.x < 1.0)){
    this.output = 2;
  }
  else {
    this.output = 3;
  }
}

您的代码不在函数中

不确定你想要实现什么

例如,您可以将代码放入构造函数中:

导出类AppComponent{ x=0;//注意,类型是推断的,因此不需要类型声明 输出=0; 建造师{ 如果这个.x<0.5{ 这个输出=1; } 否则,如果this.x>=0.5&&this.x<1.0{ 这个输出=2; } 否则{ 这个输出=3; } } }
您的代码不在函数中

不确定你想要实现什么

例如,您可以将代码放入构造函数中:

导出类AppComponent{ x=0;//注意,类型是推断的,因此不需要类型声明 输出=0; 建造师{ 如果这个.x<0.5{ 这个输出=1; } 否则,如果this.x>=0.5&&this.x<1.0{ 这个输出=2; } 否则{ 这个输出=3; } } }
<>你不能把一些语句扔在一个类的中间,并希望它能起作用。您必须创建类方法或将语句添加到类的构造函数或生命周期挂钩中


或MySufft {代码},然后从某处调用这个.MySufft。

< P>不能只在类的中间抛出一些语句,并希望它能正常工作。您必须创建类方法或将语句添加到类的构造函数或生命周期挂钩中


或者myFunct{…code},然后从某处调用此.myFunct。

谢谢!这很有道理,解决了我的问题。我没有意识到我可以声明变量,但不能执行代码,在类内部,但在函数外部。谢谢!这很有道理,解决了我的问题。我没有意识到我可以在类内部但在函数外部声明变量,但不能执行代码。
[WDS] Errors while compiling. Reload prevented.
(webpack)-dev-server/client:162
app.component.ts(10,11): error TS1005: ',' expected.
app.component.ts(10,22): error TS1005: ',' expected.
app.component.ts(10,24): error TS1003: Identifier expected.
app.component.ts(13,3): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
constructor() {
  if (this.x < 0.5){
    this.output = 1;
  }
  else if ((this.x >= 0.5) && (this.x < 1.0)){
    this.output = 2;
  }
  else {
    this.output = 3;
  }
}