简体   繁体   English

ng服务,ng构建,ng构建--prod问题

[英]ng serve, ng build, ng build --prod issues

So ng serve and ng build compile and run with no errors. 这样就可以编译并运行,并且不会出错。 When I run ng build prod it gives me the Property 'title' does not exist on type '{}'. 当我运行ng build prod时,它给我的属性'title'在类型'{}'上不存在。 I have tired implementing an interface as well and get the same errors. 我也很累实现接口,并得到相同的错误。 The code runs perfectly fine with no errors with ng serve. 该代码运行良好,没有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>`

Here is the Ts file snippet 这是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);
   }

Typescript is looking for title on recipe, but they don't exist on the Object class. Typescript在配方上寻找标题,但在Object类上不存在。 You need to declare the type of your objects as any and then Typescript won't complain about your property names. 您需要将对象的类型声明为任何对象,然后Typescript不会抱怨您的属性名称。 Like this: 像这样:

recipe: any = {};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM