我可以让 Angular 编译器在绑定不存在的 属性 时抛出错误吗?
Can I make the Angular Compiler throw an error when a non-existing property is bound?
考虑以下模板:
<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 {}
name
和 doLogin()
都不存在。它仍然编译和运行没有错误。只有当我尝试单击该按钮时,我才会在浏览器的控制台中收到错误消息 doLogin is not a function。
我知道,这种行为(运行时错误)主要是 Javascript 有意为之。不过,我希望在编译时显示尽可能多的错误。使用 Typescript 可以完成大部分工作。
但是,属性 绑定似乎不会在编译时求值。在我看来,Angular-Compiler 应该可以做到这一点。我尝试将 fulltemplatetypecheck 设置为 true,但这似乎没有用。尝试使用 ng serve
、ng build
和 ng build --prod
。
有没有办法在编译时检测不存在的 属性 绑定?如果不是,这是预期的行为吗?
我正在使用 Angular 5.2.0、Typescript 2.5.3 和 Angular CLI 1.6.4
该功能将在 angular 6 上可用。基于 Issue。
We have a new compiler setting called fullTemplateTypeCheck (see
https://github.com/angular/angular/blob/master/packages/compiler-cli/src/transformers/api.ts#L99)
which will reveal this error.
This setting will become the default in Angular 6, and is highly
recommended. We didn't turn in on in Angular 5 to not break existing
users. I.e. the behavior you are seeing here is exactly what users saw
in Angular 4.
Angular6以下
您可以使用 TS Lint 来实现这一点。
我使用 TS Lint 和 visual studio 代码来验证 non-existent 属性 绑定和方法。
It wont stop you from compiling but still show a error.
TS Lint 将验证 method,property
在组件中未找到
angular CLI 仍然可以成功编译您的代码。
考虑以下模板:
<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 {}
name
和 doLogin()
都不存在。它仍然编译和运行没有错误。只有当我尝试单击该按钮时,我才会在浏览器的控制台中收到错误消息 doLogin is not a function。
我知道,这种行为(运行时错误)主要是 Javascript 有意为之。不过,我希望在编译时显示尽可能多的错误。使用 Typescript 可以完成大部分工作。
但是,属性 绑定似乎不会在编译时求值。在我看来,Angular-Compiler 应该可以做到这一点。我尝试将 fulltemplatetypecheck 设置为 true,但这似乎没有用。尝试使用 ng serve
、ng build
和 ng build --prod
。
有没有办法在编译时检测不存在的 属性 绑定?如果不是,这是预期的行为吗?
我正在使用 Angular 5.2.0、Typescript 2.5.3 和 Angular CLI 1.6.4
该功能将在 angular 6 上可用。基于 Issue。
We have a new compiler setting called fullTemplateTypeCheck (see https://github.com/angular/angular/blob/master/packages/compiler-cli/src/transformers/api.ts#L99) which will reveal this error.
This setting will become the default in Angular 6, and is highly recommended. We didn't turn in on in Angular 5 to not break existing users. I.e. the behavior you are seeing here is exactly what users saw in Angular 4.
Angular6以下
您可以使用 TS Lint 来实现这一点。 我使用 TS Lint 和 visual studio 代码来验证 non-existent 属性 绑定和方法。
It wont stop you from compiling but still show a error.
TS Lint 将验证 method,property
在组件中未找到
angular CLI 仍然可以成功编译您的代码。