如何修复 Angular4/5/6 Unexpected token 'px'
如果你遇到类似这样的错误消息:
angular_error_example.txt
Parser Error: Unexpected token 'px' at column 3 in [70px] in ng:///AppModule/MyComponent.html@5:34 ("="let string of strings">查看错误引用的行。它看起来类似这样:
angular_broken_height.html
<mat-expansion-panel-header [collapsedHeight]="70px">你有两种修复方法:
选项 1: 如果值(在此例中为 70px)始终是常量,则推荐使用。
从属性中删除方括号:将 [collapsedHeight] 改为 collapsedHeight。方括号表示值应被解释为 Javascript,删除方括号意味着将值解释为属性。你的代码应该如下所示:
angular_fixed_height_option1.html
<mat-expansion-panel-header collapsedHeight="70px">选项 2: 强制 angular 将值(在此例中为 70px)解释为字符串:
在值前后添加单引号使其成为有效的 Javascript:
angular_fixed_height_option2.html
<mat-expansion-panel-header [collapsedHeight]="'70px'">我建议仅在你期望该值在未来成为非常量 javascript 表达式时使用此选项。
Check out similar posts by category:
Angular, Javascript
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow