所以我在这个类对象中有这个类,我实例化了它,但我无法调用它的函数。 IDE 允许我调用 getPoistionDiagram:
export class NodeW {
childrenIds: string[];
diagram?: {
coordinates: {
x: number;
y: number;
};
} | null;
nodeId: string;
parameters: {
childrens?: string[];
name?: string;
nodeNumber?: string;
original?: string;
cases?: any;
parent?: any;
extra?: any;
copyIds?: any;
copyNames?: any;
};
type: NodeType;
constructor(
childrenIds: string[],
nodeId: string,
parameters: {},
type: NodeType,
diagram?: { coordinates: { x: number; y: number } },
) {
this.childrenIds = childrenIds;
this.diagram = diagram;
this.nodeId = nodeId;
this.parameters = parameters;
this.type = type;
}
getPositionDiagram(i: number): { x: number; y: number } {
return {
x: !this.diagram ? 20 * 10 : this.diagram.coordinates.x,
y: !this.diagram ? i * 120 : this.diagram.coordinates.y,
};
}
}
export class NodesArray {
private nodes: NodeW[];
private createNewNodesService?: CreateNewNodesService;
constructor(nodes?: NodeW[]) {
if (nodes && nodes.length > 0) {
this.nodes = nodes;
} else {
this.nodes = DEFAULT_NODE_ARRAY();
}
}
getNodes(): NodeW[] {
return this.nodes;
}
setNodes(nodes: any) {
this.nodes = nodes;
}
getParentNode(nodeId: string) {
for (let i = 0; i < this.nodes.length; i++) {
if (this.nodes[i].childrenIds.includes(nodeId)) {
return this.nodes[i].nodeId;
}
}
return null;
}
}
所以当我这样做时:
this.nodes?.getNodes()!.forEach((node: NodeW, i) => {
node.getPositionDiagram(i) // => .getPoistonDiagram() is not a function
});
尝试使用我拥有的属性内的方法。它不允许我访问NodeW的方法,而且当我检查它时它说不是一个typeOf NodeW。
与 froEach() 相关吗?
我认为,您的 NodeW 类无法访问 getNodes,因为这是 NodesArray 内部的方法,因此,您无法访问 getPoisitionDiagram。您不需要访问 getNode:
this.nodes?.getNodes()!.forEach((node: NodeW, i) => {
node.getPositionDiagram(i) // => .getPositionDiagram() is not a function
});
您需要:
this.nodes.forEach((node: NodeW, i) => {
node.getPositionDiagram(i)
});