如何将JointJS与Vue 2和Webpack集成

阿米特·沙玛(Amit Sharma)

我对vue js和webpack非常陌生,想将jointjs与vue 2或webpack集成。我尝试搜索,但找不到与jointjs和vue 2相关的文章。有没有人将jointjs与Vue2或Webpack集成在一起,是否有任何示例代码可供参考?任何帮助将不胜感激。

谢谢。

奥列格·史莱夫(Oleg Shleif)

首先,您需要向项目中添加必要的组件,特别是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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章