簡體   English   中英

運行ng build時,屬性“ title”在類型“ {}”上不存在-雖然prod可與ng serve一起使用

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

當我運行ng服務時,應用程序運行良好。 當我運行ng build --prod類型{}時,一堆屬性不存在,錯誤彈出,並且我無法部署。 我相信是因為配方= {}; 當我將其設置為配方時; 從數據庫填充的下拉列表為空白,並且在表單頁面的控制台中出現了其他各種錯誤。

這是我的代碼.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);
  }
}

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

我認為Pengyy所說的是為該屬性創建一個接口(或類)。 請參見下面的代碼:

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

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

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

我認為您也可以將配方屬性設置為任何類型。 請看下面:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM