简体   繁体   English

angular中用mermaid-js处理异步数据

[英]Handling asynchronous data with mermaid-js in angular

So, I am working on a project where I need to get data from the DB asynchronously and then use that data to draw a sequence diagram using mermaid.js' markdown syntax.因此,我正在开展一个项目,我需要从数据库异步获取数据,然后使用该数据使用 mermaid.js 的 markdown 语法绘制序列图 But the problem that I'm facing is that apparently the sequence diagram is rendered in the UI during the initial loading time itself and then it shows the diagram as a simple HTML content.但我面临的问题是,序列图显然是在初始加载期间在 UI 中呈现的,然后它将图显示为简单的 HTML 内容。 I tried the setTimeOut() to pause the mermaid.initialize() for a second or so and then execute it but it does not work as well.我尝试使用 setTimeOut( )暂停mermaid.initialize() 大约一秒钟,然后执行它,但效果不佳。

However, if there are no backend calls to the db, ie, the data is hard-coded then the sequence diagram works just fine.但是,如果没有对数据库的后端调用,即数据是硬编码的,那么序列图就可以正常工作。

I also tried keeping startOnLoad property as false, but again it shows the diagram as a simple HTML content.我还尝试将 startOnLoad 属性保持为 false,但它再次将图表显示为简单的 HTML 内容。

Any idea on how to handle this particular use case?关于如何处理这个特定用例的任何想法?

Case 1: When the data is hard-coded.情况 1:当数据被硬编码时。

.html file .html 文件

<div class="mermaid">
  sequenceDiagram
  {{final}}
</div>

.ts file .ts 文件

import { Component, OnInit } from '@angular/core';
import * as mermaid from "mermaid";

@Component({
 selector: 'app-mermaid',
 templateUrl: './mermaid.component.html',
 styleUrls: ['./mermaid.component.css']
})
export class MermaidComponent implements OnInit {

 constructor() { }
 final:string = '';
 items=[
 { 
  source:"1",
  destination:"2",
  message:"Request from 1"
},
{ 
  source:"2",
  destination:"1",
  message:"Response from 2"
 }
]


ngOnInit(): void {
 mermaid.initialize({
  theme: "forest"
}
);

for(let i=0; i<this.items.length; i++) {
  this.final += `Node${this.items[i].source}->>Node${this.items[i].destination}: ${this.items[i].message};`
  }
 }
}

For this case, a sequence diagram is rendered in the UI:对于这种情况,在 UI 中呈现了一个序列图:

when data is hard coded, the sequence diagram is this.当数据被硬编码时,序列图是这样的。

Case 2: When the data is received from a db.情况 2:当从数据库接收到数据时。

Here, instead of calling data from a db, I have simply created a setTimeOut in my ngOnInit(), to imitate an asynchronous call.在这里,我没有从数据库调用数据,而是在我的 ngOnInit() 中简单地创建了一个 setTimeOut,以模仿异步调用。

  ngOnInit(): void {
mermaid.initialize({
  theme: "forest"
}
);
setTimeout(() => {
  for(let i=0; i<this.items.length; i++) {
    this.final += `Node${this.items[i].source}->>Node${this.items[i].destination}: ${this.items[i].message};`
  }
},100)

} }

Here, I get some svg error, I tried to solve it with and additional property in initialize() ie;在这里,我得到了一些 svg 错误,我试图用initialize()中的附加属性来解决它,即;

mermaid.initialize({
  theme: "forest",
  startOnLoad: false,
}

But, this renders the string as it is.但是,这会按原样呈现字符串。 Somewhat like this.有点像这样。

sequenceDiagram Node1->>Node2: Request from 1;Node2->>Node1: Response from 2; sequenceDiagram Node1->>Node2: Request from 1;Node2->>Node1: Response from 2;

Any ideas on how to solve this?关于如何解决这个问题的任何想法?

So, I found a solution to this.所以,我找到了解决办法。 I used a mermaid.init() function along with a setTimeOut() of a second.我使用了mermaid.init() function 和一秒钟的 setTimeOut() 。 It solves the issue for now.它暂时解决了这个问题。

This is what I did.这就是我所做的。

ngOnInit(){
  mermaid.initialize({
    theme: "forest",
    startOnLoad: false,
  }
  );
  setTimeout(() => {
    for(let i=0; i<this.items.length; i++) {
      this.final += `Node${this.items[i].source}->>Node${this.items[i].destination}: ${this.items[i].message};`
    }
  },1000)

   setTimeout(() => {
     mermaid.init()
   },1000)
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM