声明前使用的变量

时间:2017-08-29 06:55:18

标签: angular typescript tslint

我使用angular-cli生成了我的项目,版本为1.0.0-beta.28.3 我在终端中写了一个命令ng lint并得到大量错误:

enter image description here

我没有任何想法,为什么在运行ng lint后出现这样的金额错误。

machines.component.ts的代码:

import { Component, OnDestroy, OnInit } from '@angular/core';
import { Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { DialogService } from 'ng2-bootstrap-modal';

import { ModalComponent } from '../modal/modal.component';
import { MachineService } from '../services/machine.service';
import { Machine } from '../models/machine.model';
import { BoxService } from '../services/box.service';
import { Box } from '../models/box.model';
import { AppConfig } from '../app.config';

@Component({
  selector: 'app-machines',
  templateUrl: './machines.component.html',
  styleUrls: ['./machines.component.scss']
})
export class MachinesComponent implements OnInit, OnDestroy {

  private display: boolean;
  private alive: boolean;
  private timer: Observable<number>;
  machines: Machine [];
  box: Box;

  constructor(
    private dialogService: DialogService,
    private boxService: BoxService,
    private machineService: MachineService,
    private appConfig: AppConfig) {
    this.display = false;
    this.alive = true;
    this.timer = Observable.timer(0, this.appConfig.interval_requests);
  }

  ngOnInit() {
    this.timer
      .takeWhile(() => this.alive)
      .subscribe(() => {
        this.machineService.getStatesMachines().subscribe(
          (res: Response) => {
            this.machines = res.json();

            if (!this.display) {
              this.display = true;
            }
          }
        );
      }
    );
  }

  ngOnDestroy() {
    this.alive = false;
  }

  getBox(device_name: string) {
    this.boxService.getBox(device_name).subscribe(
      (res: Response) => {
        const box = res.json();
        this.box = box;

        this.dialogService.addDialog(ModalComponent, {
          device_name: this.box.device_name + '`s info',
          name: this.box.name,
          timestamp: this.box.timestamp,
          ip_address: this.box.ip_address
        }, {closeByClickingOutside: true});
      }
    );
  }

}

生成项目时,我没有更改tslint.json中的任何属性。默认值为"no-use-before-declare": true

我在Github和Stackoverflow搜索了答案,但没有找到。也许,如果到目前为止还没有找到,我搜索得很糟糕  请帮我。

1 个答案:

答案 0 :(得分:3)

您可以设置"no-use-before-declare": false

使用var关键字时,此规则主要有用 - 编译器将在声明

之前检测是否使用了letconst变量