首页 > 解决方案 > 将负数 -1 传递给文字数字类型为 1| 的方法 -1 来自 html 错误

问题描述

我的html:

<button (mousedown)="onMouseDown($event, -1)"></button>

我的ts:

  onMouseDown(e: MouseEvent, direction: 1|-1) {
    this.compute.emit(direction);
    this._mouseDown.next({e, direction});
  }

当我将参数 1 传递给 onMouseDown 时,一切正常当我通过错误将 -1 传递给 onMouseDown aot 编译时:

number-control.component.html(11,11): : 'number' 类型的参数不能分配给'vector' 类型的参数

似乎 html 无法理解文字类型。我该如何处理?

标签: htmlangulartypescript

解决方案


在这种特殊情况下,Angular Ivy 与联合类型定义冲突。禁用它将是一种解决方法。尝试以下

tsconfig.ts

{
  "angularCompilerOptions": {
    "enableIvy": false,
     ...
   }
}

推荐阅读