简体   繁体   English

我如何在 vue.js 中使用 SVG.js 插件?

[英]How do i use SVG.js plugins in vue.js?

I am using svg.js in my Laravel project running vue.js .我在运行vue.js svg.js

This is how i use svg.js这就是我使用svg.js的方式

Step 1: Install svg.js as a plugin in my vue app.第 1 步:在我的 vue 应用程序中安装svg.js作为插件。

import svgJs from "svg.js/dist/svg"

export default {
    install(Vue) {
        Vue.prototype.$svg = svgJs
    }
}

Step 2: Import and use the plugin.第二步:导入并使用插件。

import svgJs from "./plugins/vueSvgPlugin"

Vue.use(svgJs);

Step 3: Then i can do this.第 3 步:然后我可以做到这一点。

console.log(this.$svg);

console.log(this.$svg.get("samplesvg"));

However i am not sure how to add the svg.js plugins.但是我不确定如何添加svg.js插件。 I want to use below three plugins, in case someone wants to know.我想使用以下三个插件,以防有人想知道。

  1. svg.select.js svg.select.js
  2. svg.resize.js svg.resize.js
  3. svg.draggable.js svg.draggable.js

I have my solution for working with non npm / module library.我有使用非npm / module库的解决方案。

First I will use jsdelivr to serve file from directly from Github.首先,我将使用jsdelivr直接从 Github 提供文件。 For example https://cdn.jsdelivr.net/npm/svg.js@2.7.1/dist/svg.min.js .例如https://cdn.jsdelivr.net/npm/svg.js@2.7.1/dist/svg.min.js

Then I use script.js to load them.然后我使用script.js加载它们。

script.order([
  "https://cdn.jsdelivr.net/npm/svg.js@2.7.1/dist/svg.min.js",
  "https://cdn.jsdelivr.net/npm/svg.select.js@3.0.1/dist/svg.select.min.js"
], () => {
  // window.SVG is available
});

Live Example实例

To use version 3.x you would either just do it with esm imports:要使用 3.x 版,您要么只使用 esm 导入:

import { SVG } from '@svgdotjs/svg.js'
import '@svgdotjs/svg.draggable.js'
// ...

No need to use plugins for vue.无需为 vue 使用插件。 Just use SVG() in your code when you need it.只需在需要时在代码中使用SVG() If you need other functionality lile the "old" SVG.on() you need to import it, too: import { SVG, on } from '@svgdotjs/svg.js'如果您需要其他功能,例如“旧的” SVG.on() ,您也需要导入它: import { SVG, on } from '@svgdotjs/svg.js'

Or you include it via script tags.或者您通过脚本标签包含它。 svg.js always ships with prebundled files for that purpose:为此,svg.js 总是附带预先捆绑的文件:

<script src="node_modules/@svgdotjs/svg.js/dist/svg.js"></script>
<script src="node_modules/@svgdotjs/svg.draggable.js/dist/svg.draggable.js"></script>

You can just use the global SVG variable in that case and access everything with it like SVG.on(...)在这种情况下,您可以只使用全局SVG变量并使用它访问所有内容,例如SVG.on(...)

I had to change my approach according to comment by Fuzzyma我不得不根据Fuzzyma的评论改变我的方法

So i just did simple import in my app file, and it all worked fine.所以我只是在我的应用程序文件中做了简单的导入,一切正常。 It is also worth mentioning that i used versions < 3.0 to make it work.还值得一提的是,我使用版本< 3.0来使其工作。

import * as SVG from 'svg.js/dist/svg';
import './plugins/svg.draggable/dist/svg.draggable';
import './plugins/svg.select/dist/svg.select';
import './plugins/svg.resize/dist/svg.resize';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM