javascript - Vue2 导出 SVG 元素以作为文件下载

标签 javascript vue.js svg vuejs2 vue-component

codepen使用 pur javacript 下载 SVG 作为文件。我想用 Vue.js 来做到这一点。我有一个 svg,其中一个属性 radius 绑定(bind)到 vue 实例数据,并通过 slider 动态控制。我希望能够通过单击按钮随时将当前 svg 元素下载为 svg 文件。

模板,

<svg id="dynamicIllustration" version="1.1" baseProfile="full" width="300" height="200">
<rect width="100%" height="100%" fill="lightblue" />
<circle cx="150" cy="100" :r="radius" fill="lightgreen" />
<text x="150" y="125" font-size="60" text-anchor="middle" fill="gray">SVG</text>
</svg>
<button class="btn btn-primary btn-sm" @click="downloadSVG()">Download SVG</button>
<input style="display: inline-block;" type="range" min="0" max="100" width="100" class="form-control-range sliderSize" v-model="radius" id="formControlRange">

我如何编写使用 vue 执行此操作的方法?

  downloadSVG(){
        console.log("running downloadSVG()");
        // document.querySelector(".link-download").addEventListener("click", (evt) => {
    const svgContent = document.getElementById("dynamicIllustration").outerHTML,
          blob = new Blob([svgContent], {
              type: "image/svg+xml"
          }),
          url = window.URL.createObjectURL(blob),
          link = evt.target;

    link.target = "_blank";
    link.download = "Illustration.svg";
    link.href = url;
// });

      },

有一行被注释掉了,因为点击事件已经被vue处理了。因此,我没有 evt,这就是我尝试运行它时出现的错误信息。

谢谢,

最佳答案

Vue 会自动将事件传递给您的点击处理程序

  <a href="#" @click="downloadSVG" class="link-download btn btn-primary btn-sm">Download displayed SVG</a>

你只需要像这样在你的方法中接收它:

    downloadSVG(evt){....

Vue.config.devtools = false;
Vue.config.productionTip = false;

new Vue({
  el: '#app',
  data: {
    radius: 10
  },
  methods: {
    downloadSVG(evt) {


      const svgContent = document.getElementById("dynamicIllustration").outerHTML,
        blob = new Blob([svgContent], {
          type: "image/svg+xml"
        })
      let url = window.URL.createObjectURL(blob);
      let link = evt.target;

      link.target = "_blank";
      link.download = "Illustration1.svg";
      link.href = url;
      let body = document.querySelector('body')
      body.appendChild(link)
      link.click()
      console.log(link)

    }

  }

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css" />



<div id="app" class="container">
  <svg id="dynamicIllustration" version="1.1" baseProfile="full" width="300" height="200">
<rect width="100%" height="100%" fill="lightblue" />
<circle cx="150" cy="100" :r="radius" fill="lightgreen" />
<text x="150" y="125" font-size="60" text-anchor="middle" fill="gray">SVG</text>
</svg>
  <a href="#" @click="downloadSVG" class="link-download btn btn-primary btn-sm">Download displayed SVG</a>

  <input style="display: inline-block;" type="range" min="0" max="100" width="100" class="form-control-range sliderSize" v-model="radius" id="formControlRange">

</div>

关于javascript - Vue2 导出 SVG 元素以作为文件下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52794027/

相关文章:

vue.js - 如何在 vue tel 输入中获取国家/地区代码?

php - 使用 php、mysql 和 vuejs 动态创建文件的下载链接

jquery - SVG 动画在 IE9/IE10 中不工作,但在 Firefox 和 Chrome 中工作

javascript - SVG 矩形的笔划宽度在顶部/左侧被 chop 。我该如何解决?

javascript - jQuery slideToggle() 中的特定对象

javascript - Gulp watch,等待任务

javascript - 在 .vue 组件内嵌套内联模板

python - 如何从 svgpathtools 贝塞尔曲线获取坐标列表?

javascript - 使用 JQuery 设置名为 "required"的属性和任何值都不起作用

javascript - 通过 javaScript 获取 chrome 中当前的事件语言 (Chrome 66 Stable)