SoFunction
Updated on 2025-04-06

Vue-cli3 generated Vue project loading Mxgraph method example

Use Vue-cli3 to generate a Vue project and wait for the project creation to be successful.

vue create [Project name]

Install mxgraph.

cnpm install mxgraph --save

Install exports-loader.

cnpm install exports-loader --save

Install script-loader.

cnpm install script-loader --save

Create a new file in the project root directory.

Copy the following to the file.

const path = require('path');

function resolve(dir) {
  return (__dirname, dir);
}

 = {
  publicPath: './',
  outputDir: 'dist',
  lintOnSave: true,
  chainWebpack: (config) => {
    
      .rule('')
      .test(/mxClient\.js$/)
      .use('exports-loader')
      .loader('exports-loader?mxClient,mxGraphModel,mxActor,mxShape,mxEventObject,mxGraph,mxPrintPreview,mxEventSource,mxRectangle,mxVertexHandler,mxMouseEvent,mxGraphView,mxImage,mxGeometry,mxRubberband,mxKeyHandler,mxDragSource,mxGraphModel,mxEvent,mxUtils,mxWindow,mxEvent,mxCodec,mxCell,mxConstants,mxPoint,mxGraphHandler,mxCylinder,mxCellRenderer,mxEvent,mxUndoManager')
      .end();
    
      .set('@', resolve('src'))
      .set('@assets', resolve('src/assets'));
    // Add it yourself in this format.set('', resolve(''))  }
};

Modify, replace with the following content.

<template>
  <div ref="graph_container"></div>
</template>

<script>
import {
  mxGraph
} from 'mxgraph/javascript/mxClient';

export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  mounted() {
    // Creates the graph inside the given container
    var graph = new mxGraph(this.$refs.graph_container);

    // Gets the default parent for inserting new cells. This
    // is normally the first child of the root (ie. layer 0).
    var parent = ();

    // Adds cells to the model in a single step
    ().beginUpdate();
    try {
      let v1 = (parent, null, 'Hello,', 20, 20, 80, 30);
      let v2 = (parent, null, 'World!', 200, 150, 80, 30);

      (parent, null, '', v1, v2);
    } finally {
      // Updates the display
      ().endUpdate();
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  display: inline-block;
  margin: 0 10px;
}

a {
  color: #42b983;
}
</style>

Run the project to see the effect. thisDemoThe source code can be viewed

This is the article about the example of Vue project loading Mxgraph method generated by Vue-cli3. For more related Vue project loading Mxgraph content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!