简体   繁体   English

运行ng build时,属性“ title”在类型“ {}”上不存在-虽然prod可与ng serve一起使用

[英]Property 'title' does not exist on type '{}' when running ng build --prod works with ng serve though

When I run ng serve the application run fine. 当我运行ng服务时,应用程序运行良好。 When I run ng build --prod a bunch of Property does not exist on type {} errors pop up, and I cannot deploy. 当我运行ng build --prod类型{}时,一堆属性不存在,错误弹出,并且我无法部署。 I believe it is because recipe = {}; 我相信是因为配方= {}; When I set it just to recipe; 当我将其设置为配方时; the drop down that gets populated from the database is blank, and various other errors come up in the console on the form page. 从数据库填充的下拉列表为空白,并且在表单页面的控制台中出现了其他各种错误。

Here is my code .ts file 这是我的代码.ts文件

import { RecipeService } from './../recipe.service';
import { Observable } from 'rxjs/Observable';
import { CategoryService } from './../category.service';
import { Component } from '@angular/core';
import { AngularFireObject, AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { FirebaseListObservable } from "angularfire2/database-deprecated";
import { FormGroup, FormControl, FormArray } from '@angular/forms';
import { FirebaseApp } from 'angularfire2';
import { RecurseVisitor } from '@angular/compiler/src/i18n/i18n_ast';
import { Router, ActivatedRoute } from '@angular/router';
import 'rxjs/add/operator/take';

@Component({
  selector: 'app-recipe-form',
  templateUrl: './recipe-form.component.html',
  styleUrls: ['./recipe-form.component.css']
})
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);
   }

   save(recipe){
      recipe.ingredientsList = this.form.value;
      if(this.id) this.recipeService.update(this.id, recipe);
      else this.recipeService.create(recipe);
      this.router.navigate(['/recipes']);
      //  console.log(recipe);     
    }



  addIngredient(ingredient: HTMLInputElement) {
    this.ingredients.push(new FormControl(ingredient.value));
    ingredient.value = '';
  }

  getList(){
    return this.form.get('ingredients') as FormArray;
  }

  get ingredients() {
    return this.form.get('ingredients') as FormArray;
  }


  removeIngredient(ingredient: FormControl){
    let index = this.ingredients.controls.indexOf(ingredient);
    this.ingredients.removeAt(index);
  }
}

Here is the html form: 这是html形式:

<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>

      <div class="form-group">
          <label for="source">Recipe Source</label>
          <input #source="ngModel" [(ngModel)]="recipe.source" name="source" id="source" type="source" class="form-control" required>
          <div class="alert alert-danger" *ngIf="source.touched && source.invalid">
            Source is required
          </div>
        </div>

      <div class="form-group">
        <label for="category">Category</label>
        <select #category="ngModel" [(ngModel)]="recipe.category" name="category" id="category" class="form-control" required>
          <option value=""></option>
          <option *ngFor="let c of categories$ | async" [value]="c.key">
            {{ c.name }}
          </option>      
        </select>
        <div class="alert alert-danger" *ngIf="category.touched && category.invalid">
          Category is required
        </div>
      </div>

      <div class="form-group">
        <label for="ingredientsList">Ingredients</label>
        <div class="input-group mb-3">
          <input #ingredientsList="ngModel" ngModel name="ingredientsList" id="ingredientsList" type="text" class="form-control" (keyup.enter)="addIngredient(ingredient)" #ingredient required>
          <div class="input-group-append">
            <button type="button" class="input-group-text fa fa-plus" (click)="addIngredient(ingredient)"></button>
          </div>
        </div>
          <div *ngIf="recipe.ingredientsList">
            <ul class="list-group">
              <li 
                *ngFor="let i of recipe.ingredientsList.ingredients"
                (click)="removeIngredient(i)"                
                class="list-group-item">
                {{ i }}
              </li>
            </ul>
          </div>

          <ul class="list-group">
            <li
              *ngFor="let i of ingredients.controls"
              (click)="removeIngredient(i)"
              class="list-group-item">
              {{ i.value }}
            </li>
          </ul>
        <div class="alert alert-danger" *ngIf="ingredientsList.touched && ingredientsList.invalid">
          Ingredients are required
        </div>
      </div>

      <div class="form-group">
          <label for="directions">Directions</label>
          <textarea #directions="ngModel" [(ngModel)]="recipe.directions" name="directions" id="directions" class="form-control"  rows="3" required></textarea>
          <div class="alert alert-danger" *ngIf="directions.touched && directions.invalid">
              Directions are required
          </div>
      </div>

      <div class="form-group">
        <label for="imageUrl">Image URL</label>
        <input #imageUrl="ngModel" [(ngModel)]="recipe.imageUrl" name="imageUrl" id="imageUrl" type="text" class="form-control" required url>
        <div class="alert alert-danger" *ngIf="imageUrl.touched && imageUrl.invalid">
          <div *ngIf="imageUrl.errors.required">Image URL is required</div>
          <div *ngIf="imageUrl.errors.url">Please enter valid URL</div>      
        </div>
      </div>

      <button (click)="save(f.value)" type="button" class="btn btn-primary">Save</button>
    </form>
  </div>

  <div class="col-md-6">
    <div class="card" style="width: 18rem;">
      <img class="card-img-top" [src]="recipe.imageUrl" *ngIf="recipe.imageUrl">
      <div class="card-body">
        <h5 class="card-title">{{ recipe.title }}</h5>
        <p class="card-text">{{ recipe.source }} </p>
      </div>
    </div>
  </div>  
</div>

I think what Pengyy is saying is to create an interface (or class) for the property. 我认为Pengyy所说的是为该属性创建一个接口(或类)。 Please see code below: 请参见下面的代码:

export class Recipe implements IRecipe {
    source: any;
    ingredients: any[];
}

export interface IRecipe {
    source: any;
    ingredients: any[];
}

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

I think you can also set the recipe property to the any type. 我认为您也可以将配方属性设置为任何类型。 Please see below: 请看下面:

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

暂无
暂无

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

相关问题 使用ng build --prod的类型不存在属性“ NameNews” - Property 'NameNews' does not exist on type using ng build --prod 运行 ng build --output-hashing=all --prod 时,类型“AbstractControl”上不存在属性“controls” - Property 'controls' does not exist on type 'AbstractControl' while running ng build --output-hashing=all --prod 当我尝试使用 ng build --prod 构建应用程序时,它说“属性 'invited_by' 在类型 'any[]' 上不存在” - When i try to build app using ng build --prod it says “Property 'invited_by' does not exist on type 'any[]' ” Angular 应用程序编译成功,但在“ng build --prod”中不存在提供错误属性 - Angular app is compiling successfully but giving errors property does not exist in 'ng build --prod' TS2334类型“对象”上不存在属性“长度”。 仅在应用启动时(ng-serve) - TS2334 Property 'length' does not exist on type 'Object'. on app start only (ng-serve) Angular 8:运行“ng build --prod”时core.ts中的错误 - Angular 8: erros in core.ts when running "ng build --prod" Angular 4 ng serve --prod vs ng serve - Angular 4 ng serve --prod vs ng serve 带有 --prod 标志的 Ionic 5 构建失败 - 类型上不存在属性“翻译” - Ionic 5 build with --prod flag fails - Property 'translate' does not exist on type 如何在 Angular 2 CLI 中使用“ng build --prod”和“ng serve --prod”,得到 404 错误 - How to use “ng build --prod” and “ng serve --prod” with Angular 2 CLI, getting 404 errors 运行ng serve Angular5时出错 - Error when running ng serve Angular5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM