【发布时间】: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