1. 程式人生 > 程式設計 >Vue-cli3生成的Vue專案載入Mxgraph方法示例

Vue-cli3生成的Vue專案載入Mxgraph方法示例

使用Vue-cli3生成Vue專案,並等待專案建立成功。

vue create [專案名]

安裝mxgraph。

cnpm install mxgraph --save

安裝exports-loader。

cnpm install exports-loader --save

安裝script-loader。

cnpm install script-loader --save

在專案根目錄新建vue.config.js檔案。

將以下內容複製到vue.config.js檔案中。

const path = require('path');

function resolve(dir) {
  return path.join(__dirname,dir);
}

module.exports = {
  publicPath: './',outputDir: 'dist',lintOnSave: true,chainWebpack: (config) => {
    config.module
      .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,mxEvent,mxUtils,mxWindow,mxCodec,mxCell,mxConstants,mxPoint,mxGraphHandler,mxCylinder,mxCellRenderer,mxUndoManager')
      .end();
    config.resolve.alias
      .set('@',resolve('src'))
      .set('@assets',resolve('src/assets'));
    // 按這種格式.set('',resolve('')) 自己新增
  }
};

修改HelloWorld.vue,替換為以下內容。

<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 = graph.getDefaultParent();

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

      graph.insertEdge(parent,'',v1,v2);
    } finally {
      // Updates the display
      graph.getModel().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>

執行專案,檢視效果。此Demo的原始碼可以檢視

到此這篇關於Vue-cli3生成的Vue專案載入Mxgraph方法示例的文章就介紹到這了,更多相關Vue專案載入Mxgraph內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!