【问题标题】:variable used before declaration声明前使用的变量
【发布时间】:2018-02-06 12:36:47
【问题描述】:

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

我不知道为什么在运行 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 上搜索了答案,但没有找到。也许,如果到目前为止没有找到,我搜索它很糟糕。
请帮我。

【问题讨论】:

  • 不如先升级到最新的稳定版ng-cli,以及自带的tslint版本。自从您使用过时的测试版以来,可能已经带来了很多错误和改进。
  • 我更新了angular-cli

标签: angular typescript tslint


【解决方案1】:

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

此规则主要在使用 var 关键字时有用 - 编译器将在声明之前检测是否使用了 letconst 变量

【讨论】:

  • 我本来想设置"no-use-before-declare": false,但最后我决定如果我更新一个版本angular-cli,这个功能会更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-07
  • 1970-01-01
  • 1970-01-01
  • 2014-09-04
  • 1970-01-01
  • 2020-08-11
相关资源
最近更新 更多