本文介绍了角度5-“无法读取未定义的属性"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular 5中,输入

In Angular 5, for input

<input name="techSpecMeta.make" [(ngModel)]="techSpecMeta.make" type="text" class="form-control border-input" placeholder="Enter car brand">

获取错误无法读取未定义的属性"make" 在Object.eval [作为updateDirectives]

getting errorCannot read property 'make' of undefined at Object.eval [as updateDirectives]

export class UserComponent implements OnInit {
  constructor(private vahinfo: VehicleInfo) {}
  ngOnInit() {}
  techSpecMeta: {};
  onSave = function(vehicle, isValid: boolean) {
    this.vahinfo.saveVehicle(vehicle).subscribe(data => {
      console.log(data.data)
    }, error => this.errorMessage = error)
  }
}

推荐答案

techSpecMeta: {};

在类型脚本中,这意味着声明类型为{}的属性,且未初始化任何值.它与:

In Type script this means to declare a property of type {} with no value initialized. It is the same as:

techSpecMeta: Object;

您应该这样做

techSpecMeta = {};

要使绑定生效,您还将需要属性make.

To make the binding work, you will need the property make as well.

techSpecMeta = {make: null};

理想情况下,您将为TechSpecMeta创建一个类/接口

Ideally you would create a class/interface for TechSpecMeta

class TechSpecMeta {
    make: null;
    anotherProperty: null;
}

并在您的组件中使用

techSpecMeta = new TechSpecMeta();

这篇关于角度5-“无法读取未定义的属性"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-25 22:42