我有一个使用的应用程序:
它包括:
父组件: src/app/search.component.ts
它包含一个搜索栏和一些对服务器的API调用将显示按钮的区域。
子组件: src/app/search-details.component.ts
<div>
组成
醇>
在子组件的SearchDetailsComponent
类中有一个名为nodeClicked
的简单函数,如下所示:
nodeClicked(e, node) {
// show the name of the clicked node on the console
console.log(node.data.text);
this.checkString = node.data.text // HERE is the ERROR....
}
每次渲染图表并且用户点击任何节点时,都会出现错误:
TypeError:这是未定义的
Stack-Trace:
execute/SearchDetailsComponent</SearchDetailsComponent.prototype.nodeClicked@http://run.plnkr.co/L7H44HHRrMceJAkj/src/app/search-details.component.ts!transpiled:96:21
Vh@https://unpkg.com/gojs/release/go-debug.js:539:43
Vg.prototype.standardMouseClick@https://unpkg.com/gojs/release/go-debug.js:537:201
ek.prototype.doMouseUp@https://unpkg.com/gojs/release/go-debug.js:667:73
E.prototype.doMouseUp@https://unpkg.com/gojs/release/go-debug.js:802:92
Ll/a.no@https://unpkg.com/gojs/release/go-debug.js:1012:149
ZoneDelegate.prototype.invokeTask@https://unpkg.com/zone.js/dist/zone.js:424:17
onInvokeTask@https://unpkg.com/@angular/core/bundles/core.umd.js:3956:28
ZoneDelegate.prototype.invokeTask@https://unpkg.com/zone.js/dist/zone.js:423:17
Zone.prototype.runTask@https://unpkg.com/zone.js/dist/zone.js:191:28
ZoneTask/this.invoke@https://unpkg.com/zone.js/dist/zone.js:486:28
它所引用的this
是checkString
变量在类SearchDetailsComponent
中声明的那个。
如何识别checkString
?我在上面定义了它。此错误会一直显示 API服务我希望每次点击该节点时都会调用此错误,并且也会调用 HTTP GET 。
我在这里做错了什么?
答案 0 :(得分:1)
Node.click 属性是一个在不绑定this
的情况下调用的函数。
如果需要从事件处理程序中访问外部组件,您当然可以指定一个闭包的nodeClicked
函数。
答案 1 :(得分:0)
正如@WalterNorthwoods的回答所暗示的那样。我为Node.click
创建了一个内联函数,如下所示:
click: (e: go.InputEvent, obj: go.GraphObject) => { this.nodeClicked(e, obj); }
nodeClicked
函数现在可以调用,并且this.checkString
值显示得非常完美。