Angular Typescript类显示为对象,缺少某些类成员

问题描述 投票:0回答:1

为什么将打字稿类放在参数中,显示为对象?

((1)显然已将其标记为类,但在调试器中显示为对象。

(2)另外,某些班级成员丢失;如果它们丢失,则应显示为null而不是不出现

我该如何解决?

在Visual Studio代码中更新:有时Visual Studio代码将其显示为对象。其他时间为null,

enter image description here

export class PropertySitusAddress {

    primaryPropertyMailingAddressId?:number;
    propertyId?:number;
    propertySitusAddressId?:number;
    addressFormatId?:number;

    apn?: string;
    owner?: string;
    situsAddress?:string;
    primaryMailingAddress?:string;

    streetNumber?: string;
    situsFromNumber?: string;
    situsThroughNumber?: string;
    fractional?: string;
    predirectional?: string;
    streetName?: string;
    streetType?: string;

**另外,有时Visual Studio代码将其显示为对象。其他时间为null,

enter image description here

angular typescript angular8
1个回答
1
投票

这就是JavaScript中的类。一个东西。您的class方法可以在对象__proto__属性中找到。

示例:

class Person {
  constructor(first, last) {
    this.first = first;
    this.last = last;
  }
  getFullName() {
    return `${this.first} ${this.last}`;
  }
}

const person = new Person('John', 'Doe');
console.log(person);
console.log(person.__proto__.getFullName) // <-- heres your function
console.log(JSON.stringify(person)); // <-- goodbye class methods

*此问题并不仅仅与TypeScript有关。它特定于JavaScript以及类的工作方式。

在这里您可以将null中的值默认为constructor

class Person {
  constructor(first = null, last = null) { // default props to null
    this.first = first;
    this.last = last;
  }
  getFullName() {
    return `${this.first} ${this.last}`;
  }
}

const person = new Person();
console.log(person);

但是,您的属性(无价值)未显示的真正原因是,因为没有任何构造发生,并且它们是可选的。如果您要这样做,我会将所有内容默认设置为null。这是我会做的:

class PropertySitusAddress {

    primaryPropertyMailingAddressId?: number = null;
    propertyId?: number = null;
    propertySitusAddressId?: number = null;
    addressFormatId?: number = null;

    apn?: string = null;
    owner?: string = null;
    situsAddress?: string = null;
    primaryMailingAddress?: string = null;

    streetNumber?: string = null;
    situsFromNumber?: string = null;
    situsThroughNumber?: string = null;
    fractional?: string = null;
    predirectional?: string = null;
    streetName?: string = null;
    streetType?: string = null;
}

console.log(new PropertySitusAddress());

这里是一个JSFiddle,因为SOF不支持TS https://jsfiddle.net/mswilson4040/sdLkhjaq/

© www.soinside.com 2019 - 2024. All rights reserved.