当前位置:Gxlcms > JavaScript > vue+Jointjs使用教程

vue+Jointjs使用教程

时间:2021-07-01 10:21:17 帮助过:7人阅读

这次给大家带来vue+Jointjs使用教程,vue+Jointjs使用教程的注意事项有哪些,下面就是实战案例,一起来看一下。

在vue中引入joint.js的问题,之前在网上搜了很多,都没有给出一个确切的答案,捣鼓了两天终于弄明白了,做个记录。
首先,我参考了一篇来自stackoverflow的文章点我点我

看完这篇文章,大家应该至少大致怎么做了,下面我们来具体看一下:

首先在vue项目中运行npm install jointjs --save

然后在入口文件,我的是main.js,也有可能是app.js中加入下面两行,把joint.js和jquery作为全局变量

  1. window.$ = require('jquery');
  2. window.joint = require('jointjs');

这里需要注意的是,joint.js依赖backbone、jquery和lodash,在通过script方式引入时,需要一一引入这些文件,但通过vue的npm时不需要,npm引入的joint.js已经默认封装好了这些。

通过这样引入还不够,可能会遇到图可以正常加载,但无法拖拽的问题,遇到这些问题一般是joint.js和自己vue项目中的环境冲突了,导致无法读取或者读取错误。

我原来的项目中安装了element、iview、axios、vuex、jquery,再安装joint.js后,jointjs无法正常加载,后来重新建了一个项目,只安装了element、axios、vuex,为避免jquery和joint.js中的jquery冲突,后来没有装jquery。

这样就行了么?就可以运行上文链接中的例子了么?像这样:

  1. <template>
  2. <p>
  3. <h1>Home</h1>
  4. <p id="myholder"></p>
  5. </p>
  6. </template>
  7. <script>
  8. export default {
  9. created() {
  10. let graph = new joint.dia.Graph;
  11. let paper = new joint.dia.Paper({
  12. el: $('#myholder'),
  13. width: 600,
  14. height: 200,
  15. model: graph,
  16. gridSize: 1
  17. });
  18. let rect = new joint.shapes.basic.Rect({
  19. position: { x: 100, y: 30 },
  20. size: { width: 100, height: 30 },
  21. attrs: { rect: { fill: 'blue' }, text: { text: 'my box', fill: 'white' } }
  22. });
  23. let rect2 = rect.clone();
  24. rect2.translate(300);
  25. let link = new joint.dia.Link({
  26. source: { id: rect.id },
  27. target: { id: rect2.id }
  28. });
  29. graph.addCells([rect, rect2, link]);
  30. }
  31. }
  32. </script>

NoNoNo,注意到这里是把渲染放在了created的生命周期里,根据vue的生命周期,是无法找到joint的挂载p的el: $('#myholder'),也就是说,运行会报错,我的解决方法是把p放了一个click,把joint的内容从created中拿出,放在methods中,需要点击一下才可显示哦,还不太完美,以待改进(~ ̄▽ ̄)~

也就是说,代码会变成这样:

  1. <template>
  2. <p>
  3. <p id="myholder" @click="click_joint"></p>
  4. </p>
  5. </template>
  6. <script>
  7. export default {
  8. methods:{
  9. click_joint() {
  10. let graph = new joint.dia.Graph;
  11. let paper = new joint.dia.Paper({
  12. el: $('#myholder'),
  13. width: 600,
  14. height: 200,
  15. model: graph,
  16. gridSize: 1
  17. });
  18. let rect = new joint.shapes.basic.Rect({
  19. position: { x: 100, y: 30 },
  20. size: { width: 100, height: 30 },
  21. attrs: { rect: { fill: 'blue' }, text: { text: 'my box', fill: 'white' } }
  22. });
  23. let rect2 = rect.clone();
  24. rect2.translate(300);
  25. let link = new joint.dia.Link({
  26. source: { id: rect.id },
  27. target: { id: rect2.id }
  28. });
  29. graph.addCells([rect, rect2, link]);
  30. }
  31. }
  32. }
  33. </script>

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

Angular CLI怎样生成路由

Angular CLI怎样使用蓝本生成代码

以上就是vue+Jointjs使用教程的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行