Angular 6 在构建生产期间获取对象错误

Angular 6 getting object error during build production

我的开发中有一个正在运行的 angular 应用程序,它工作正常,但每当我尝试构建用于生产的站点时,我都会收到此错误:

 ERROR in src/app/agriculture/agriculture.component.html(20,61): : Property 'districts' does not exist on type '{}'.
src/app/agriculture/agriculture.component.html(27,60): : Property 'years' does not exist on type '{}'.
src/app/agriculture/agriculture.component.html(35,64): : Property 'view' does not exist on type '{}'.
src/app/agriculture/agriculture.component.html(41,64): : Property 'rain_fall_type' does not exist on type '{}'.
src/app/agriculture/agriculture.component.html(48,68): : Property 'Comparison' does not exist on type '{}'.
src/app/agriculture/agriculture.component.html(20,61): : Property 'districts' does not exist on type '{}'.
src/app/agriculture/agriculture.component.html(27,60): : Property 'years' does not exist on type '{}'.
src/app/agriculture/agriculture.component.html(35,64): : Property 'view' does not exist on type '{}'.
src/app/agriculture/agriculture.component.html(41,64): : Property 'rain_fall_type' does not exist on type '{}'.
src/app/agriculture/agriculture.component.html(48,68): : Property 'Comparison' does not exist on type '{}'.

我在组件内部使用一个对象:

data = {};

在我的 html 中,我的代码是:

 <div class="col-sm-2">
      <select class='select-option' required [(ngModel)]='data.years' name="years">
       <option [ngValue]="undefined" disabled  selected> Views  </option>
       <option class='option' *ngFor='let option of views' [ngValue]="option">{{option}}</option>
      </select>
</div>

如何解决这个问题以及为什么我在开发模式下没有收到任何错误。

在我的组件中,我使用这个数组来循环:

  views = ["Graph", "Trend Line","Map View","Table"];

您的解决方案的问题非常简单,您的数据对象未定义,这就是您收到此错误的原因。

我遇到过类似的问题,我犯了和你一样的错误。您只需将其添加到您的数据对象中:

 data: any = {};

这将解决您的问题。