Angular在兄弟组件之间共享Sigmajs图。

huangapple go评论119阅读模式
英文:

Angular share sigmajs graph between sibling components

问题

sigma.component.ts中创建了一个sigma实例。我的问题是,我如何与兄弟组件search.component.ts共享sigma实例?

通常情况下,我会使用@Input和@Output装饰器以及事件发射器来通过父组件作为桥梁共享图形。但是在sigma.component.ts模板中只有

标签,我无法通过这种方式找到解决方案。我需要sigma实例在sigma.component.ts中创建/更改时随时共享。

英文:

I am following this example to display a sigmajs graph in angular: https://github.com/sim51/ng-sigma-example.

app.component.ts

import { Component } from "@angular/core";
import Graph from "graphology";
import erdosRenyi from "graphology-generators/random/erdos-renyi";
import circularLayout from "graphology-layout/circular";

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.scss"]
})
export class AppComponent {
  title = "ng-sigma-example";
  graph: Graph;

  constructor() {
    this.graph = erdosRenyi(Graph, { order: 100, probability: 0.1 });
    circularLayout.assign(this.graph);
  }
}

sigma.component.ts

import {
  Component,
  OnDestroy,
  AfterViewInit,
  ElementRef,
  ViewChild,
  Input
} from "@angular/core";
import Graph from "graphology";
import { Sigma } from "sigma";

@Component({
  selector: "sigma",
  template: "<div #container></div>",
  styleUrls: ["./sigma.component.scss"]
})
export class SigmaComponent implements AfterViewInit, OnDestroy {
  @ViewChild("container") container: ElementRef | null = null;
  @Input("graph") graph: Graph = new Graph();
  sigma?: Sigma;

  ngAfterViewInit(): void {
    if (this.container)
      this.sigma = new Sigma(this.graph, this.container.nativeElement);
  }

  ngOnDestroy(): void {
    if (this.sigma) {
      this.sigma.kill();
    }
  }
}

app.component.html

<sigma [graph]="graph"> </sigma>

search.component.ts

import { 
  Component,
  OnInit,
  ElementRef,
  ViewChild,
  Input} from '@angular/core';
import { Sigma } from "sigma";
import Graph from "graphology";

@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.scss']
})
export class SearchComponent implements OnInit {
  @ViewChild("container") container: ElementRef | null = null; // could be null -> hence needs if statement
  @Input("graph") graph: Graph = new Graph();
  @Input() // should give me the sigma instance from sigma.component.ts

  constructor() {     
  }

  ngOnInit(): void {
      // should give me access to sigma instance graph
      console.log(this.renderer?.getGraph())

  }

}

In sigma.component.ts a sigma instance is created. My questions is, how can I share the sigma instance with a sibling component search.component.ts?

Normally, I would use @input and @output decorators with an event emitter to share the graph using the parent component as a bridge. But I only have div tags in the sigma.component.ts template and I was unable to get a solution that way. I need the sigma instance to be shared any time a new sigma instance is created/changed in sigma.component.ts

答案1

得分: 0

另一种在组件之间共享数据的方法是使用可观察对象(Observables)的服务。您可以创建一个服务,其中包含一个可以从任何地方更新的值,并可以从任何地方监听这些更改。您可以在https://angular.io/guide/observables 上阅读更多信息。

sigmaService.ts

sigmaObservable: Subject<Sigma> = new Subject();

setSigma(receivedSigma: Sigma): {
    this.sigmaObservable.next(this.receivedSigma);
}

sigma.component.ts(不要忘记导入该服务并将其添加到构造函数中)

ngAfterViewInit(): void {
    if (this.container) {
        this.sigma = new Sigma(this.graph, this.container.nativeElement);
        this.sigmaService.setSigma(this.sigma); // 或者创建一个单独的变量,并将其设置为this.sigma和这个
    }  
}

search.component.ts(创建一个订阅变量和一个sigma变量来填充。当使用订阅接收到新值时,请在ngOnInit中填充,并在ngOnDestroy中不要忘记取消订阅)

_subscription: Subscription = Subscription.EMPTY;
sigmaInSearch: Sigma;

ngOnInit(): void {
    this._subscription = this.sigmaService.sigmaObservable.subscribe((sigmaValue) => {
        this.sigmaInSearch = sigmaValue;
    });
}

ngOnDestroy(): void {
    this._subscription.unsubscribe();
}
英文:

Another way of sharing data between components is with a service using Observables. You can make a service which has a value that can be updated from anywhere and listen to those changes from anywhere. You can read more on https://angular.io/guide/observables.

sigmaService.ts

sigmaObservable: Subject&lt;Sigma&gt; = new Subject();

setSigma(receivedSigma: Sigma): {
    this.sigmaObservable.next(this.receivedSigma);
}

sigma.component.ts (don't forget to import the service and add it to the constructor)

ngAfterViewInit(): void {
    if (this.container) {
        this.sigma = new Sigma(this.graph, this.container.nativeElement);
        this.sigmaService.setSigma(this.sigma); // or create a separate variable and set it to both this.sigma and this one
    }  
}

search.component.ts (create a subscription variable and the sigma variable to populate. Populate on ngOnInit when the new value is received with a subscription and don't forget to unsubbscribe onDestroy)

_subscription: Subscription = Subscription.EMPTY;
sigmaInSearch: Sigma;

ngOnInit(): void {
    this._subscription = this.sigmaService.sigmaObservable.subscribe((sigmaValue) =&gt; {
        this.sigmaInSearch = sigmaValue;
    });
}

ngOnDestroy(): void {
    this._subscription.unsubscribe();
}

huangapple
  • 本文由 发表于 2023年3月7日 02:39:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定