This time I will show you how to use jointjs attributes in vue. What are the precautions for using jointjs attributes in vue. The following is a practical case, let's take a look.
Regarding the issue of introducing joint.js into vue, I searched a lot on the Internet before, but no definite answer was given. After two days of tinkering, I finally figured it out and made a record.First of all, I referred to an article from stack
overflow. After reading this article, you should at least have a rough idea of how to do it. Let’s take a closer look. Let’s take a look:
entry file
, mine is main.js, it may also be app.js Add the following two lines and use joint.js and jquery as global variableswindow.$ = require('jquery'); window.joint = require('jointjs');
<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>
lifecycle
of created. According to the lifecycle of vue, the el of the joint's mounting p cannot be found: $('#myholder'), that is to say, an error will be reported when running. My solution is to put a click on p, take the content of joint out of created, and put it in methods. It needs to be clicked before it can be displayed. , it is not perfect yet and needs to be improved (~ ̄▽ ̄)~In other words, the code will become like this:
<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>
Angular CLI blueprint generation code
The difference between computed and methods in Vue
The above is the detailed content of How to use jointjs attributes in vue. For more information, please follow other related articles on the PHP Chinese website!