【问题标题】:variable of Class not being recognized in the function for Angular 2 typescript with GoJS在带有 GoJS 的 Angular 2 typescript 的函数中无法识别 Class 的变量
【发布时间】:2017-07-07 14:51:53
【问题描述】:

我有一个应用程序使用:

  • 角度 2
  • 用于可视化的 GoJS

它包括:

  1. 父组件src/app/search.component.ts

    它由一个搜索栏和一些对服务器的 API 调用将显示按钮的区域组成。

  2. 子组件src/app/search-details.component.ts

    它由一个<div> 组成,它体现了GoJS Radial Layout Diagram

在子组件的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....
}

每次渲染图表并且用户单击任何节点时,都会出现错误说明:

类型错误:这是未定义的

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

这里所指的thisSearchDetailsComponent类中声明的checkString变量。

checkString 无法识别怎么办?我已经在上面定义了。每次单击节点时我希望调用 API 服务 并且也调用 HTTP GET 时,此错误不断出现。

我在这里做错了什么?

Live Plunker

Embed Plunker

【问题讨论】:

    标签: angular gojs


    【解决方案1】:

    Node.click 属性是一个无需绑定this 即可调用的函数。

    如果您需要从事件处理程序中访问外部组件,您当然可以指定一个 nodeClicked 函数作为闭包。

    【讨论】:

    • 谢谢沃尔特。再次前往救援!我打电话给内联click: (e: any, node: any) =&gt; {this.nodeClicked(e, node)},它就像一个魅力
    • 您的 arg 类型可能更精确。这是go.d.ts 中的声明: click: (e: InputEvent, obj: GraphObject) => void;
    【解决方案2】:

    正如@WalterNorthwoods 的回答所暗示的那样。我为Node.click 创建了一个内联函数,如下所示:

    click: (e: go.InputEvent, obj: go.GraphObject) => { this.nodeClicked(e, obj); }
    

    nodeClicked 函数现在可以调用,this.checkString 值显示完美。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-23
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多