【问题标题】:Vue2 Export SVG element to download as a fileVue2导出SVG元素以下载为文件
【发布时间】:2018-10-13 14:36:30
【问题描述】:

codepen 使用 pur javacript 将 SVG 下载为文件。我想用Vue.js 来做这件事。我有一个 svg,其中一个属性 radius 绑定到 vue 实例数据并通过滑块动态控制。我希望能够通过单击按钮随时将当前的 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,这就是我尝试运行它时错误告诉我的内容。

谢谢,

【问题讨论】:

    标签: javascript vue.js svg vuejs2 vue-component


    【解决方案1】:

    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>

    【讨论】:

      猜你喜欢
      • 2019-09-20
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多