Как исправить ошибку TS1251: не допускаются объявления функций внутри блоков в строгом режиме при нацеливании на «ES3» или «ES5».? - PullRequest
0 голосов
/ 06 марта 2020

У меня есть код, и я получаю следующую ошибку:

ошибка TS1251: Не допускаются объявления функций внутри блоков в строгом режиме при нацеливании на «ES3» или «ES5». Определения классов автоматически выполняются в строгом режиме.

Код:

 lineDraw(){
  flag = true;    
  var point1, isDown, x1, y1, x2, y2, x, y;
  var count = 0;
  if (canvas != undefined) {
    canvas.on('mouse:down', function (options) {
      if (!mouseEvent){
        count = 0;
        drawFlag = true;
        mouseEvent = true;
        //point1 = undefined;
        x = undefined;
        y = undefined;
      }
      if (flag) {
        getMouse(options);
      }
    });
      function getMouse(options) {
        var coords = {"x":{}, "y": {}};
        var coords1 = {"x":{}, "y": {}};
        let canvasElem = document.querySelector("canvas");
        var bounds = canvasElem.getBoundingClientRect();
        x = (options.e.clientX - bounds.left);
        y = (options.e.clientY - bounds.top);
        x /= bounds.width;
        y /= bounds.height;
        x *= canvas.width;
        y *= canvas.height;
        if (point1 === undefined) {
          point1 = new fabric.Point(x, y)
        } else {
          var line = new fabric.Line([point1.x, point1.y, x, y], {
            fill: 'red',
            stroke: 'red',
            strokeWidth: 5,
            selectable: false,
            evented: false,
            objectCaching: false
          });
          canvas.add(line);
          coords.x = parseFloat((point1.x / scaledWidth).toFixed(2));
          coords.y = parseFloat((point1.y / scaledHeight).toFixed(2));
          //coords.push((point1.x / (scaledWidth)).toFixed(2));
          //coords.push((point1.y / scaledHeight).toFixed(2));
          if (count == 0) {
            myObj.points['point' + count] = (coords);
            count = count + 1;
          }
          //console.log(y, scaledHeight);
          coords1.x=parseFloat((x / scaledWidth).toFixed(2));
          coords1.y=parseFloat((y / scaledHeight).toFixed(2));
          myObj.points['point' + count] = coords1;
          count = count + 1;
          console.log('added', point1.x / scaledWidth, point1.y / scaledHeight, x / scaledWidth, y / scaledHeight)
          point1.x = x;
          point1.y = y;
          canvas.setActiveObject(line);
        }
      }
    }
    else {
      this._snackBar.open('Load Image first before drawing', 'Close', {
        duration: 5000,
      });
    }
  }

Я пытаюсь получить доступ к этой функции и получаю эту ошибку.

1 Ответ

1 голос
/ 06 марта 2020

Уберите вашу getMouse функцию вне блока if

, чтобы вы выглядели так

lineDraw() {
  flag = true;
  var point1, isDown, x1, y1, x2, y2, x, y;
  var count = 0;
  if (canvas != undefined) {
    canvas.on('mouse:down', function(options) {
      if (!mouseEvent) {
        count = 0;
        drawFlag = true;
        mouseEvent = true;
        //point1 = undefined;
        x = undefined;
        y = undefined;
      }
      if (flag) {
        this.getMouse(options, point1);
      }
    });

  } else {
    this._snackBar.open('Load Image first before drawing', 'Close', {
      duration: 5000,
    });
  }
}

getMouse(options, point1) {
  var coords = {
    "x": {},
    "y": {}
  };
  var coords1 = {
    "x": {},
    "y": {}
  };
  let canvasElem = document.querySelector("canvas");
  var bounds = canvasElem.getBoundingClientRect();
  x = (options.e.clientX - bounds.left);
  y = (options.e.clientY - bounds.top);
  x /= bounds.width;
  y /= bounds.height;
  x *= canvas.width;
  y *= canvas.height;
  if (point1 === undefined) {
    point1 = new fabric.Point(x, y)
  } else {
    var line = new fabric.Line([point1.x, point1.y, x, y], {
      fill: 'red',
      stroke: 'red',
      strokeWidth: 5,
      selectable: false,
      evented: false,
      objectCaching: false
    });
    canvas.add(line);
    coords.x = parseFloat((point1.x / scaledWidth).toFixed(2));
    coords.y = parseFloat((point1.y / scaledHeight).toFixed(2));
    //coords.push((point1.x / (scaledWidth)).toFixed(2));
    //coords.push((point1.y / scaledHeight).toFixed(2));
    if (count == 0) {
      myObj.points['point' + count] = (coords);
      count = count + 1;
    }
    //console.log(y, scaledHeight);
    coords1.x = parseFloat((x / scaledWidth).toFixed(2));
    coords1.y = parseFloat((y / scaledHeight).toFixed(2));
    myObj.points['point' + count] = coords1;
    count = count + 1;
    console.log('added', point1.x / scaledWidth, point1.y / scaledHeight, x / scaledWidth, y / scaledHeight)
    point1.x = x;
    point1.y = y;
    canvas.setActiveObject(line);
  }
}

JavaScript не имеет видимости блока, имеет только область действия функции. Таким образом, не имеет значения, если вы пишете определение функции внутри, если это блок или нет, он доступен во всем включающем функциональном блоке.

Таким образом, машинопись выдает эту ошибку, так как это может придать читателю неоднозначное значение, что функция только доступно внутри блока if

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...