首页 > 解决方案 > 在另一个类角度4的模型中使用类对象?

问题描述

在我的项目中,我在模型部分有两个类,它们想要在另一个中使用其中一个的对象。其中一类是:

export class ConditionPerson {
    marriedStatus: number;
    familyNum: number;
    pet: number;
    describtion: string;
}

我想在下面的类中使用这个类的对象:

 import {ConditionPerson } from './ConditionPerson ';

 export class Property{
    condition : ConditionPerson ;
 }

现在我想在另一个类中使用条件属性:

import {Property} from '../models/Property';
export class AddPropertyComponent implements OnInit {
constructor(public:property: Property){}

if (this.addPropertyPage2.selectedStatus) {
    if (this.addPropertyPage2.selectedStatus != null && typeof this.addPropertyPage2.selectedStatus === 'string') {
        this.property.condition.marriedStatus= +this.addPropertyPage2.selectedVazTaahol;
    } 
    else {
           this.property.conditon.marriedStatus= this.addPropertyPage2.selectedStatus.id;
    }
  }
}

在 if 的 else 中,当它为 property.condition.marriedStatus 分配 id 时说: 无法设置未定义的属性“条件” 我应该怎么做才能解决这个问题?感谢您在高级方面的帮助。

标签: angulartypescript

解决方案


初始化属性对象

 this.property = new property();

推荐阅读