绑定不存在的属性时,是否可以使Angular Compiler抛出错误?

时间:2018-03-21 09:39:44

标签: angular typescript angular-compiler

考虑以下模板:

<p>Hello, {{ name }}</p>
<button (click)="doLogin()">Login</button>

与此TS-Class一起使用:

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {}

namedoLogin()都不存在。它仍然编译并运行没有错误。只有当我尝试单击按钮时,我才会在浏览器的控制台中收到错误消息 doLogin不是函数

我知道,这种行为(运行时出错)主要是由Javascript实现的。不过,我希望在编译时尽可能多地显示错误。使用Typescript可以完成大部分工作。

但是,在编译时似乎不会评估属性绑定。在我看来,Angular-Compiler应该能够做到这一点。我尝试将fulltemplatetypecheck设置为true,但这似乎并没有成功。通过ng serveng buildng build --prod来尝试。

有没有办法在编译时检测不存在的属性绑定?如果不是,这是预期的行为吗?

我使用的是Angular 5.2.0,Typescript 2.5.3和Angular CLI 1.6.4

1 个答案:

答案 0 :(得分:2)

此功能将在角度6上可用。基于Issue

  

我们有一个名为fullTemplateTypeCheck的新编译器设置(参见   https://github.com/angular/angular/blob/master/packages/compiler-cli/src/transformers/api.ts#L99)   这将揭示这个错误。

     

此设置将成为Angular 6中的默认设置,并且非常高   推荐的。我们没有在Angular 5中转而没有破坏现有的   用户。即你在这里看到的行为正是用户所看到的   在Angular 4中。

Angular 6

您可以使用TS Lint来实现此目的。 我使用TS Lint和visual studio代码来验证不存在的属性绑定和方法。

  

它不会阻止你编译但仍然显示错误。

TS Lint将验证组件中未找到的method,property

Ts Lint showing error in visual studio code

角度CLI仍然可以成功编译您的代码。