您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

vue中使用jointjs详细说明

2024/3/12 7:07:05发布19次查看
这次给大家带来vue中使用jointjs详细说明,vue中使用jointjs的注意事项有哪些,下面就是实战案例,一起来看一下。
看完这篇文章,大家应该至少大致怎么做了,下面我们来具体看一下:
首先在vue项目中运行npm install jointjs --save
然后在入口文件,我的是main.js,也有可能是app.js中加入下面两行,把joint.js和jquery作为全局变量
window.$ = require('jquery'); 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。
这样就行了么?就可以运行上文链接中的例子了么?像这样:
<template>   <p>     <h1>home</h1>     <p id="myholder"></p>   </p> </template> <script>   export default {     created() {       let graph = new joint.dia.graph;       let paper = new joint.dia.paper({         el: $('#myholder'),         width: 600,         height: 200,         model: graph,         gridsize: 1       });       let rect = new joint.shapes.basic.rect({         position: { x: 100, y: 30 },         size: { width: 100, height: 30 },         attrs: { rect: { fill: 'blue' }, text: { text: 'my box', fill: 'white' } }       });       let rect2 = rect.clone();       rect2.translate(300);       let link = new joint.dia.link({         source: { id: rect.id },         target: { id: rect2.id }       });       graph.addcells([rect, rect2, link]);     }   } </script>
nonono,注意到这里是把渲染放在了created的生命周期里,根据vue的生命周期,是无法找到joint的挂载p的el: $('#myholder'),也就是说,运行会报错,我的解决方法是把p放了一个click,把joint的内容从created中拿出,放在methods中,需要点击一下才可显示哦,还不太完美,以待改进(~ ̄▽ ̄)~
也就是说,代码会变成这样:
<template>   <p>     <p id="myholder" @click="click_joint"></p>   </p> </template> <script>   export default {    methods:{      click_joint() {       let graph = new joint.dia.graph;       let paper = new joint.dia.paper({         el: $('#myholder'),         width: 600,         height: 200,         model: graph,         gridsize: 1       });       let rect = new joint.shapes.basic.rect({         position: { x: 100, y: 30 },         size: { width: 100, height: 30 },         attrs: { rect: { fill: 'blue' }, text: { text: 'my box', fill: 'white' } }       });       let rect2 = rect.clone();       rect2.translate(300);       let link = new joint.dia.link({         source: { id: rect.id },         target: { id: rect2.id }       });       graph.addcells([rect, rect2, link]);     }    }   } </script>
点明一下,通过npm引入只要install jointjs就可以,不需要install lodash、backbone、jquery,也不需要在页面中导入joint.css文件。笔者之前通过script方式引入joint.js,试了很多次,都没有成功,一直读取joint.js文件出错,如果其他小伙伴尝试成功,欢迎交流分享。
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
angular cli进行单元与e2e测试步骤详解
computed与methods使用详解
以上就是vue中使用jointjs详细说明的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product