错误类型错误:无法读取未定义的属性“标题”

TOLULOPE ADETULA

几天来我一直在与这些代码块作斗争,即使当我尝试将它初始化为对象时,我也会出错。

这是我的 restaurantForm.ts 文件

import { Component, OnInit } from '@angular/core';
import {RestaurantService} from '../../restaurant.service';
import {ProductService} from '../../product.service';
import {ActivatedRoute, Router} from '@angular/router';
import 'rxjs/add/operator/take';
import {Product} from '../../model/product';

@Component({
  selector: 'app-product-form',
  templateUrl: './restaurant-form.component.html',
  styleUrls: ['./restaurant-form.component.css']
})
export class RestaurantFormComponent implements OnInit {
  restaurants$;
  id;
  product: Product;
  constructor(private restaurantService: RestaurantService,
              private productService: ProductService,
              private route: ActivatedRoute,
              private router: Router) {
    this.restaurants$ = this.restaurantService.getAll();
    this.id = this.route.snapshot.paramMap.get('id');
    if (this.id) {
      this.productService.get(this.id).take(1).subscribe(p => this.product = p);
    }
   this.product = new Product();
  }
  save(product) {
    if (this.id) {
      this.productService.update(this.id, product);
    } else {
      this.productService.create(product);
    }
    this.router.navigate(['/admin/restaurants']);
  }
  delete() {
    if (!confirm('Are you sure you want to delete this product?')) { return ; }
      this.productService.delete(this.id);
      this.router.navigate(['/admin/restaurants']);
  }

  ngOnInit() {
  }

}

这是我的产品型号

export interface Product {
  $key: string;
  title: string;
  price: number;
  restaurant: string;
  imageUrl: string;

}

我的餐厅表格.html

<div class="container">
  <div class="row">
    <div class="col-md-6">
      <form #f="ngForm" (ngSubmit)="save(f)">
        <div class="form-group">
          <label for="title">Title</label>
          <input #title="ngModel" [(ngModel)]="product.title" name="title" id="title" type="text" class="form-control" required>
          <div class="alert-danger alert" *ngIf="title.touched && title.invalid">
            Title is required.
          </div>
        </div>
        <div class="form-group">
          <label for="price">Delivery Price</label>
          <div class="input-group">
            <span class="input-group-addon">₦</span>
            <input #price="ngModel" [(ngModel)]="product.price" name="price" id="price"
                   type="number" class="form-control" required [min]="0">
          </div>
          <div class="alert alert-danger" *ngIf="price.touched && price.invalid">
            <div *ngIf="price.errors.required">
              Delivery Price is required
            </div>
            <div *ngIf="price.errors.min">
              Delivery Price should be 0 or higher.
            </div>
          </div>
        </div>
        <div class="form-group">
          <label for="restaurant">Restaurant</label>
          <select #restaurant="ngModel" [(ngModel)]="product.restaurant" name="restaurant" id="restaurant" class="form-control" required>
            <option value=""></option>
            <option *ngFor="let r of restaurants$ | async" [value]="r.$key">
              {{ r.name }}
            </option>
          </select>
          <div class="alert alert-danger" *ngIf="restaurant.touched && restaurant.invalid">
            Please select a restaurant.
          </div>
        </div>
        <div class="form-group">
          <label for="imageUrl">Image Url</label>
          <input #imageUrl="ngModel" [(ngModel)]="product.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 a valid Url.</div>
          </div>
        </div>
        <button  class="btn btn-primary">Save</button>
        <button type="button" (click)="delete()" class="btn btn-danger">Delete</button>
      </form>
    </div>
    <div class="col-md-6">
     <app-product-card [product]="product" [showActions]="false"></app-product-card>
    </div>
  </div>
 </div>

我在价格、$key、餐厅和 imageUrl 上遇到了同样的错误。提前致谢。尽管我查找了一些解决方案,说我应该使用 elvis Operator,例如 'product?.title',但这种方法仍然无效。

萨吉塔兰

因为 product is undefined,您需要使用constructoror内部的空对象声明和初始化它ngOninit

编辑:

您需要在组件中将产品声明为,

const product : Produce = {  $key: "", title: "",price:0,restuarant :"" ,imageurl:"" };

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

错误类型错误:无法读取未定义的属性(读取“标题”)

错误错误:未捕获(承诺):类型错误:无法读取未定义的属性“标题”

类型错误:无法读取未定义的属性(读取“类型”)

类型错误:无法读取未定义错误的属性“_id”

角度:错误类型错误:无法读取未定义的属性___

错误类型错误:无法读取未定义的属性“列表”

错误类型错误:无法读取未定义的属性

渲染错误:“类型错误:无法读取未定义的属性‘’”

错误类型错误:无法读取未定义的属性“...”

错误类型错误:无法读取未定义的属性“填充”

错误类型错误:无法读取未定义的属性“stateName”

“类型错误:无法读取未定义的属性‘名称’”错误

错误类型错误:无法读取未定义的属性“_id”

错误类型错误:无法读取未定义的属性“getUsers”

错误类型错误:无法读取未定义的属性“doc”

**错误** 类型错误:无法读取未定义的属性“替换”

类型错误:无法读取未定义的属性(读取“图像”)

类型错误:无法读取未定义的属性(读取“暗”)

类型错误:无法读取未定义的属性(读取“缓存”)

类型错误:无法读取未定义的属性(读取“reduce”)

类型错误:无法读取未定义读取“2”的属性

类型错误:无法读取未定义的属性“类型”

未捕获(承诺)类型错误:无法读取未定义的属性“标题”

类型错误:无法读取未定义值的属性“0”。标题 [0]

类型错误:无法读取 React 上未定义的属性“标题”

类型错误:无法读取未定义的属性“标题”。Vue.js

类型错误:无法读取未定义的属性“标题”-帮助我:(

类型错误:无法读取 ReactJS Jest 测试中未定义的属性“标题”

类型错误:无法读取未定义的 React Hooks 道具问题的属性“标题”和地图