当我运行服务时,应用程序运行正常。当我运行build build --prod时,类型{}上不存在一堆Property会弹出错误,我无法部署。我相信这是因为recipe = {};当我把它设置为食谱;从数据库填充的下拉列表为空白,表单页面控制台中出现各种其他错误。
这是我的代码.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>
答案 0 :(得分:0)
我认为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;
}