首页 > 解决方案 > ng 服务、ng 构建、ng 构建 --prod 问题

问题描述

所以 ng serve 和 ng build 编译和运行没有错误。当我运行 ng build prod 时,它给了我“{}”类型的属性“标题”不存在。我也厌倦了实现接口并得到相同的错误。该代码运行良好,ng serve 没有错误。

`<div class="row">
  <div class="col-md-6">
    <form #f="ngForm" (ngSubmit)="save(f.value)">
      <div class="form-group">
        <label for="title">Title</label>
        <input #title="ngModel" [(ngModel)]="recipe.title" name="title" id="title" type="text" class="form-control" required>
        <div class="alert alert-danger" *ngIf="title.touched && title.invalid">
          Title is required
        </div>
      </div>`

这是 Ts 文件片段

export class RecipeFormComponent {
  categories$: Observable<any>;
  recipe = {};
  id;

  form = new FormGroup({
    ingredients: new FormArray([])
  });

  constructor(
    private categoryService: CategoryService, 
    private recipeService: RecipeService,
    private router: Router,
    private route: ActivatedRoute) {

    this.categories$ = categoryService.getCategories();
    this.id = this.route.snapshot.paramMap.get('id');
    if(this.id) this.recipeService.get(this.id).take(1).subscribe(r => this.recipe = r);
   }

标签: angularformsngmodel

解决方案


Typescript 正在寻找配方上的标题,但它们在 Object 类中不存在。您需要将对象的类型声明为 any,然后 Typescript 不会抱怨您的属性名称。像这样:

recipe: any = {};


推荐阅读