Angular 5 "ng build" 显示没有错误,但 "ng build --prod" 显示很多错误。

4

我知道这个问题之前已经被问过了,但是我读的所有内容都没有帮助。

在执行ng build之后,没有错误。

在执行'ng build --prod'之后。

ERROR in src/app/catalog/product/product.component.html(19,43): : Property 'length' does not exist on type 'object'.
src/app/catalog/product/product.component.html(36,39): : Property 'specs' does not exist on type 'Object'.
src/app/catalog/product/product.component.html(58,8): : Property 'config' does not exist on type 'ProductComponent'. Did you mean 'Configs'?
src/app/catalog/product/product.component.html(59,14): : Property 'config' does not exist on type 'ProductComponent'. Did you mean 'Configs'?
src/app/catalog/product/add/add.component.html(14,8): : Property 'pre_configs' does not exist on type 'object'.
src/app/catalog/product/add/add.component.html(30,28): : Property 'sets' does not exist on type 'object'.
src/app/catalog/product/add/add.component.html(70,32): : Property 'price' does not exist on type 'object'.
src/app/catalog/product/add/add.component.html(70,32): : Property 'price' does not exist on type 'object'.
src/app/navigation/mini/mini-search/mini-search.component.html(9,5): : Property 'searchText' does not exist on type 'MiniSearchComponent'.
src/app/navigation/mini/mini-search/mini-search.component.html(13,5): : Property 'searchText' does not exist on type 'MiniSearchComponent'.
src/app/catalog/single/single.component.html(5,24): : Property 'id' does not exist on type 'any[]'.
src/app/catalog/single/single.component.html(74,25): : Property 'id' does not exist on type 'any[]'.
src/app/navigation/maga/maga.component.html(5,8): : Property 'name' does not exist on type 'any[]'.

这是一个非常烦人的问题。我开始逐个查看错误,并将继续这样做,但这些错误似乎没有意义,而且这种方法将花费我数小时的时间。有人能指导我从哪里开始或为什么在使用--prod后会出现新错误吗?

更新:其中一个错误示例

在我的组件html中

<div *ngIf="sample?.specs">
    <div class="spec-section" *ngFor="let section of sample.specs">
        <h2>{{section.name}}</h2>
    </div>
</div>

ng build --prod 后出现错误。

ERROR in ../product/product.component.html(37,12): : Property 'specs' does not exist on type 'Object'.

以下是获取sample组件的代码。
constructor(public configsService: ConfigsService) {
    this.sample = configsService.sample_data;
}

在执行ng serve或部署ng build后,前端看起来/运行得很好。错误只在执行ng build --prod期间出现。

1
将在组件模板(html)中访问的属性设置为“public”。 - FAISAL
1
我之前遇到过类似错误的情况,可能是因为您试图从模板中访问组件中被设置为私有的属性或函数。私有属性只能从声明它们的类中访问。AOT(--prod build)强制执行此规则。 - mgm87
公共的没有起作用。 - Omar
我觉得我需要有人查看这些错误,并告诉我是否有任何问题突出。 - Omar
你能发布你的ts文件吗? - Dan R.
显示剩余8条评论
1个回答

1
每个错误都有自己的修复方法,大多与我从服务中获取数据的方式有关。

修复示例1

constructor(public configsService: ConfigsService) {
    this.sample = configsService.sample_data;
}

应该是...
get sample() { return this.configsService.Sample_data; }
constructor(public configsService: ConfigsService) {}

为了让这个工作,我必须在服务中添加一个get。
import { Sample } from '../_data/sample_product';

@Injectable()
export class ConfigsService {
    sample_data = Sample;
    constructor()  {}
    get Sample_data() { return this.sample_data; }
}

修复示例2

html

<a (click)="config=!config" class="button">Configure</a>

这段代码在 ng serveng build 中可以运行,但在 ng build --prod 中无法运行,因为我没有在组件中声明 config。 像这样 config:boolean;

(answer.helped) ? upvote() : comment();


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接