在Ionic 2中使用NodeJS.Timer时找不到命名空间NodeJS

Chr*_*ris 18 ionic-framework ionic2 angular

我试图将我在https://github.com/bevacqua/dragula/issues/289#issuecomment-277143172上找到的代码用于我的Ionic项目.

当我运行代码时,我得到一个错误Cannot find namespace 'NodeJS',错误引用touchTimeout: NodeJS.Timer;

如何调整下面的代码以使NodeJS.Timer线路正常工作?

import { Directive, ElementRef, HostListener } from '@angular/core';

@Directive({ selector: '[delayDragLift]' })
export class DelayDragLiftDirective {

    dragDelay: number = 200; // milliseconds
    draggable: boolean = false;
    touchTimeout: NodeJS.Timer;

    @HostListener('touchmove', ['$event'])
    // @HostListener('mousemove', ['$event'])
    onMove(e: Event) {
        if (!this.draggable) {
            e.stopPropagation();
            clearTimeout(this.touchTimeout);
        }
    }

    @HostListener('touchstart', ['$event'])
    // @HostListener('mousedown', ['$event'])
    onDown(e: Event) {
        this.touchTimeout = setTimeout(() => {
            this.draggable = true;
        }, this.dragDelay);
    }

    @HostListener('touchend', ['$event'])
    // @HostListener('mouseup', ['$event'])
    onUp(e: Event) {
        clearTimeout(this.touchTimeout);
        this.draggable = false;
    }

    constructor(private el: ElementRef) {
    }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*der 39

打开src/tsconfig.app.json*.

添加"node""types"数组.

例:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "baseUrl": "./",
    "module": "es2015",
    "types": [
      "node"
    ]
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}
Run Code Online (Sandbox Code Playgroud)

*如果此文件不存在,请将指定的部分添加到tsconfig.json根文件夹中.


cha*_*ham 18

解决此问题的快速方法在这里

基本上分别改变setTimeoutclearIntervalwindow.setTimeoutwindow.clearInterval。例如,你onDown变成:

onDown(e: Event) {
    this.touchTimeout = window.setTimeout(() => {
        this.draggable = true;
    }, this.dragDelay);
}
Run Code Online (Sandbox Code Playgroud)

然后,您的声明变为:

this.touchTimeout: number | undefined;
Run Code Online (Sandbox Code Playgroud)

  • 这应该被接受,因为您不应该将节点类型包含到面向浏览器的 Angular 项目中。 (5认同)