如何访问和读取接口对象上的属性

问题描述

代码 section_1 中的方法在父组件中,当我在浏览器控制台的 measurementCoordinates显示接收到的事件的内容时:

onMeasurementCoordinatesReceived:-> [object Object]

但是当我尝试访问接收到的事件的任何属性时,我收到 undefined

请告诉我如何访问和读取接口对象上的属性

代码

onMeasurementCoordinatesReceived(measurementCoordinates: ICoordinates) {
    console.log("onMeasurementCoordinatesReceived:-> " + measurementCoordinates);
}

chils 组件中的接口

export interface ICoordinates {
    originLng: number;
    originLat: number;
    destinationLng: number;
    destinationLat: number;
}

解决方法

首先,您可以使用 "," 而不是 "+" 来控制台.记录字符串 + 对象,例如: console.log("string",object)

否则js会使用默认的toString "[object Object]"

您可以像访问任何其他对象一样访问属性,就像: objectName.propertyName