简体   繁体   English

类型“{}”上不存在属性“title”

[英]Property 'title' does not exist on type '{}'

Angular Angular

In the first few tags I have an input tag input #title = "ngModel" [(ngModel)]="product.title" this code where I am trying to use two way binding to be able to edit my form and values that I get from the firebase database.在前几个标签中,我有一个输入标签 input #title = "ngModel" [(ngModel)]="product.title" 这段代码我试图使用两种方式绑定来编辑我的表单和值从 firebase 数据库中获取。 I created an empty product object in product-form.component.ts but I get the property type error.我在 product-form.component.ts 中创建了一个空产品 object 但我收到属性类型错误。 I am not sure why the empty product object causing error because the tutorial I'm watching has the same approach.我不确定为什么空产品 object 会导致错误,因为我正在观看的教程采用相同的方法。 Goal is to be able to use that empty product object or an alternate approach to be able to work with two way binding目标是能够使用该空产品 object 或能够使用双向绑定的替代方法

product-form.component.ts
import { ProductService } from './../../product.service';
import { CategoryService } from './../../category.service';
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { take } from 'rxjs/operators';

@Component({
  selector: 'app-product-form',
  templateUrl: './product-form.component.html',
  styleUrls: ['./product-form.component.css']
})
export class ProductFormComponent implements OnInit {
  categories$;
  product = {};


  constructor(
    private route: ActivatedRoute,
    private router: Router,
    private categoryService: CategoryService, 
    private productService: ProductService) 
    {
    this.categories$ = categoryService.getCategories();
    let id = this.route.snapshot.paramMap.get('id');
    if (id) this.productService.get(id).pipe(take(1)).subscribe(p => this.product = p);
    }

   save(product){
     this.productService.create(product);
     this.router.navigate(['/admin/products']);
   }

  ngOnInit(): void {
  }

}

product-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)]="product.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="price">Price</label> 
                <div class="input-group">
                    <span class="input-group-text">$</span>
                    <input #price="ngModel" ngModel name ="price" id = "price" type="number" class="form-control" required>
                </div>
                <div class="alert alert-danger" *ngIf="price.touched && price.invalid">
                    <div *ngIf="price.errors.required">Price is required.</div>
                    <div *ngIf="price.errors.min">Price should be 0.</div>
                </div>

            </div>

            <div class="form-group">
                <label for="category">Category</label>
                <select #category="ngModel" ngModel name="category" id = "category" type="text" class="form-control" required>
                <option  value=""></option>
                <option *ngFor = "let c of categories$ | async" [value] = "c.name">
                    {{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="imageUrl">Image Url</label>
                <input #imageUrl="ngModel" ngModel name= "imageUrl" id = "imageUrl" type="url" class="form-control" required>
                <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">Invalid URL</div>
                </div>
            </div>

            <button class="btn btn-primary">Save</button>

        </form>
    </div>
    <div class="col-md-6">
        <div class="card" style="width: 18rem;">
            <img [src]="imageUrl.value" class="card-img-top">
            <div class="card-body">
              <h5 class="card-title">{{title.value}}</h5>
              <p class="card-text">{{price.value | currency:'USD':true}}</p>
            </div>
          </div>
    </div>

</div>


product.service.ts

import { Injectable } from '@angular/core';
import { AngularFireDatabase } from '@angular/fire/database';

@Injectable({
  providedIn: 'root'
})
export class ProductService {

  constructor(private db: AngularFireDatabase) { }

  create(product){
    return this.db.list('/products').push(product);

    // const itemsRef = this.db.list('products');
    // return itemsRef.push(product);
  }

  getAll() {
    return this.db.list('/products').valueChanges();

  }

  get(productId){
    return this.db.object('/products/' + productId).valueChanges();
  }
}



There are 2 ways to do this有两种方法可以做到这一点

Approach 1方法一

Declare an interface声明一个接口

export interface Product {
  title: string
}

Change the Component code section as follows更改组件代码部分如下

from

product = {};

To

product:Product;

Approach 2 - This can have side effects方法 2 - 这可能有副作用

Change HTML换HTML

From

<input #title = "ngModel" [(ngModel)]="product.title" name = "title" id = "title" type="text" class="form-control" required>

To

<input #title = "ngModel" [(ngModel)]="product['title']" name = "title" id = "title" type="text" class="form-control" required>

I fixed this error by changing a couple of things in 'tsconfig.json' file.我通过更改“tsconfig.json”文件中的一些内容来修复此错误。

First remove "strict":true and add instead of it "noImplicitUseStrict":true (Don't forget adding the comma)首先删除"strict":true并添加代替它"noImplicitUseStrict":true (不要忘记添加逗号)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM