[英]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.