2017-04-19 122 views
4

我對vue js和webpack非常新,並且想要將jointjs與vue 2或webpack集成在一起。我嘗試過搜索,但無法找到與jointjs和vue 2相關的文章。 有沒有人將jointjs與Vue2或webpack集成在一起,是否有任何示例代碼可供參考? 任何幫助將不勝感激。如何集成jointjs與Vue 2和webpack

謝謝。

+0

你如何設想他們被整合?一般來說,你會爲這樣的東西編寫[wrapper組件](https://vuejs.org/v2/examples/select2.html)。 –

回答

2

首先,你需要添加必要的組件到項目中,特別是jointjs本身npm install jointjs --save(我使用npm),然後它依賴的包也通過npm(在我看來骨幹和其他.. )。下一步,我們將它們連接到VUE所連接的文件等(我有這個app.js)以下例子:

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

在任何組件,我可以已經以這種方式let graph = new joint.dia.Graph;運行的組件使用該文件通過webpack(我有build.js,不要忘記在模板中附加這個文件)。

PS 在下面的例子中,沒有所有的依賴和它不工作,這些都是

我app.js我的代碼只是一個例子:

import Vue from 'vue' 
import VueRouter from 'vue-router' 
import Vuex from 'vuex' 

Vue.use(Vuex); 
Vue.use(VueRouter); 

import Home from './Components/Home.vue' 
import Users from './Components/Users.vue' 
import Calculator from './Components/Calculate.vue' 

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

import { store } from './store' 

const routes = [ 
    { path: '/', component: Home }, 
    { path: '/users/:id?', component: Users }, 
    { path: '/calculator', component: Calculator } 
]; 

const router = new VueRouter({ 
    mode: 'history', 
    routes 
}); 

Vue.component('index', require('./Components/Test.vue')); 

const app = new Vue({ 
    el: '#app', 
    router, 
    store 
}); 

我webpack.config .js文件

"use strict"; 

module.exports = { 
    entry: './js/app.js', 
    output: { 
     filename: './js/bundle.js' 
    }, 
    resolve: { 
     alias: { 
      vue: 'vue/dist/vue.js' 
     } 
    }, 
    module: { 
     rules: [ 
      { 
       test: /\.vue$/, 
       loader: 'vue-loader' 
      } 
     ] 
    } 
}; 

我的組件/ Home.vue

<template> 
    <div> 
     <h1>Home</h1> 
     <div id="myholder"></div> 
    </div> 
</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>