ng build --prod:运算符'=='无法应用于类型'string'和'number'

时间:2018-01-24 17:32:23

标签: angular typescript single-page-application

我正在构建一个Angular 4,它在开发人员服务器中完全运行,并且没有ng build --prod标志,但是只要我在My Angular 4 Project上运行userHotelLength = '0'; ,它就会提供以下输出:

PS:它是使用ng cli 1.4.2版本构建的

给出错误的变量:

Date: 2018-01-24T17:25:24.719Z                                                        
Hash: a7059ea62cc4eeb37fde
Time: 12033ms
chunk {0} styles.c9da94e47a8635066285.bundle.css (styles) 142 kB {3} [initial] [rendered]
chunk {1} polyfills.3bc34265385d52184eab.bundle.js (polyfills) 86 bytes {3} [initial] [rendered]
chunk {2} main.e402deade8b026b7d50e.bundle.js (main) 84 bytes [initial] [rendered]
chunk {3} inline.9d07561b59257af76b95.bundle.js (inline) 1.45 kB [entry] [rendered]

ERROR in ng:///home/x/y/z.html (10,6): Operator '==' cannot be applied to types 'string' and 'number'.
ERROR in ng:///home/x/y/z.html (29,6): Operator '!=' cannot be applied to types 'string' and 'number'.
ERROR in ng:///home/x/y/z.html (36,6): Operator '==' cannot be applied to types 'string' and 'number'.

输出:

<div class="my-booking-card loading-card" *ngIf="userHotelLength == 0 && (!userCancelledHotels || !userBookedHotels)">
...
</div>
<div *ngIf="userHotelLength != 0 && (userCancelledHotels || userBookedHotels)">

...

</div>

<div class="no-hotel-found" *ngIf="userHotelLength == 0">
...
</div>

代码:

_

2 个答案:

答案 0 :(得分:1)

当您将一种类型与 AOT 进行比较时,它会产生错误,因为它在构建时编译。

我的改变变量类型它会起作用。在使用AOT编译代码之前,你不会知道,因为在开发时通常不会使用AOT。

答案 1 :(得分:0)

找到答案:

将变量类型更改为any而不是string,因为我们正在比较右:

userHotelLength: any = '0';