Подтвердить что ты не робот

Не удается найти пространство имен NodeJS при использовании NodeJS.Timer в Ionic 2

Я пытаюсь использовать некоторый код, который я нашел на 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) {
    }
}
4b9b3361

Ответ 1

Откройте src/tsconfig.app.json *.

Добавьте "node" в массив "types".

Пример:

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

* если этот файл не существует, добавьте указанную деталь в tsconfig.json в корневой папке.

Ответ 2

Быстрый способ решить эту проблему здесь.

В основном измените setTimeout и clearInterval на window.setTimeout и window.clearInterval, соответственно. Например, ваш onDown становится:

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

Затем ваша декларация становится:

this.touchTimeout: number | undefined;

Ответ 3

Для меня, решите включить typeRoots член в compilerOptions от tsconfig.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "baseUrl": "./",
    "module": "es2015",
    "typeRoots": [
         "node_modules/@types"
     ]
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}