标识符“必需”未定义。 '__type'不包含此类成员

时间:2017-11-24 04:21:36

标签: angular angularfire2

我需要声明这个变量来输入任何?这显示在我的HTML模板中的可视代码编辑器中。

enter image description here

产品form.component.html

<div class="row">

  <div class="col-md-6">
      <form #form="ngForm" (ngSubmit)="save(form.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-addon">$</span>
                  <input #price="ngModel" 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">Price is required.</div>
                  <div *ngIf="price.errors.min">Price should be 0 or higher.</div>
                </div>
            </div>

            <div class="form-group">
                <label for="category">Category</label>
                <select #category="ngModel" ngModel [(ngModel)]="product.category" name="category" id="category" class="form-control" required>
                  <option value=""></option>
                  <option *ngFor="let category of categories$ | async" [value]="category.key">{{ category.payload.val().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 [(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>

          </form>
  </div>

  <div class="col-md-6">
      <div class="card" style="width: 20rem;">
          <img class="card-img-top" [src]="product.imageUrl" *ngIf="product.imageUrl">
          <div class="card-block">
            <h4 class="card-title">{{ product.title }}</h4>
            <p class="card-text">{{ product.price | currency: 'USD': symbol }}</p>
          </div>
        </div>
  </div>

</div>

产品form.component.ts

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

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

  categories$;
  product: any = {};

  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private categoryService: CategoryService,
    private productService: ProductService) {
    this.categories$ = categoryService.getCategories();

    let id = this.route.snapshot.paramMap.get('id');
    if (id) {
      this.productService.get(id).take(1).subscribe(p => this.product = p);
    }
  }

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

  ngOnInit() {
  }

}

如何删除此错误?应用程序中的功能似乎没有受到影响。所以根据我的理解,它编译成有效的JS。我能够通过将其声明为“任何”

来修复对象

如果可能的话,我会对学习如何为此设置接口感兴趣。

10 个答案:

答案 0 :(得分:23)

尝试添加“?”在“价格”之后。

像这样:

<div *ngIf="price?.errors.required">Price is required.</div>
<div *ngIf="price?.errors.min">Price should be 0 or higher.</div>

答案 1 :(得分:22)

角度有一个错误。如果你像这样编写* ngIf,那编译器错误就会消失:

          <div *ngIf="price.errors['required']">

答案 2 :(得分:2)

var tokenValidationParameters = new TokenValidationParameters(); tokenValidationParameters.IssuerSigningKey = new X509SecurityKey(new X509Certificate2(certLocation)); //error here

答案 3 :(得分:1)

我的VS代码有相同的问题。我只能在条件之前使用Sub UpdateChartFormat() With ActiveWorkbook.Sheets("MHFA Summary").ChartObjects("Chart 4").Activate With ActiveChart.SeriesCollection(1).DataLabels _ .ShowPercentage = True With ActiveChart.SeriesCollection(1).DataLabels _ .Separator = "" & Chr(10) & "" End With End With End With With ActiveWorkbook.Sheets("MHFA Summary").ChartObjects("Chart 1").Activate With ActiveChart.SeriesCollection(1).DataLabels _ .ShowPercentage = True With ActiveChart.SeriesCollection(1).DataLabels _ .Separator = "" & Chr(10) & "" End With End With End With 来解决它。

尝试:

!!

更多信息:https://github.com/angular/vscode-ng-language-service/issues/126

答案 4 :(得分:1)

使用hasError()方法而不是errors属性可以为我解决此问题。
像这样使用它:

 <div *ngIf="price.hasError('required')">Price is required.</div>

答案 5 :(得分:0)

您可以在媒体资源前面添加问号

  <label [attr.data-error]="Password.errors!=null?(Password?.errors.Required?'Required 
  field!':'Minimum 3 Characters Needed'):''">Password</label>

答案 6 :(得分:0)

尝试使用FormArray访问视图上FormGroup内的ngFor控件时发生类似的错误。

此问题的某些答案部分起作用,从而导致编译错误或项目构建失败。例如:

libraryForm.get('books') //compile error
libraryForm.controls['books'].controls' //build failure

到目前为止,只有以下解决方案对我有用:

libraryForm['controls']['books']['controls']

整个元素:

<div [formGroupName]="i" *ngFor="let book of libraryForm['controls']['books']['controls'];let i = index">

答案 7 :(得分:0)

我遇到了同样的问题,得益于AntónioCésarJúnior的回答,您得以解决:

https://stackoverflow.com/a/53403416/8992452  仅使用

   !!

<div *ngIf="submitted && f.username.errors">
    <p *ngIf="f.username.errors.required">Email is required</p>
    <p *ngIf="f.username.errors.email">The mail address is invalid</p>
</div>

在上一个项目中工作,然后更改为:

 <div *ngIf="submitted && !!f.username.errors">
   <p *ngIf="!!f.username.errors.required">Email is required</p>
   <p *ngIf="!!f.username.errors.email">The mail address is invalid</p>
 </div>

答案 8 :(得分:0)

请检查此解决方案(对我来说效果很好)!

<div class="alert alert-danger" *ngIf="username.touched && username.invalid">
  <div *ngIf="username.errors['required']">Username are required!</div>
  <div *ngIf="username.errors['minlength']">Username should be minimum {{ username.errors['minlength'].requiredLength }} characters!</div>
  <div *ngIf="username.errors['cannotContainSpace']">Username cannot contains spaces!</div>
</div>

答案 9 :(得分:0)

docs中似乎提到了一些问题:

有时,绑定表达式会在AOT编译期间触发类型错误,并且不可能或很难完全指定类型。要使错误消失,可以使用$ any()强制转换函数将表达式强制转换为任何类型,如以下示例所示

所以您可以这样做:

$any(price.errors).required