简体   繁体   English

将交互式 SVG 动态加载到 Vue.js 项目中

[英]Dynamically loading interactive SVGs into Vue.js project

I have an old project built years ago with spaghetti jQuery code, which I'd like to update to use a JS framework (mostly for the usual reasons: better scaleability, better state management, cleaner/more maintainable code, etc.).我有一个多年前用意大利面条 jQuery 代码构建的旧项目,我想更新它以使用 JS 框架(主要是出于通常的原因:更好的可扩展性、更好的状态管理、更清洁/更可维护的代码等)。 I find Vue.js especially intriguing.我发现 Vue.js 特别有趣。

The project allows the user to load any of a large selection of .svg files from a directory on the server into the DOM dynamically, then interact with them (mostly clicking to change fill/line colors of individual path elements, handled by jQuery plus a plugin ).该项目允许用户从服务器上的目录中将任何大量 .svg 文件动态加载到 DOM 中,然后与它们交互(主要是单击以更改单个路径元素的填充/线条颜色,由 jQuery 处理加上插件)。

After some research and experimentation, I haven't found a simple way to do this with Vue--as best I can tell, I would need to manually edit each .svg file to put the (many) bindings in place to allow for discrete parts of the .svg to have interactivity.经过一些研究和实验,我还没有找到使用 Vue 的简单方法——据我所知,我需要手动编辑每个 .svg 文件以放置(许多)绑定以允许离散.svg 的部分具有交互性。 This is a dealbreaker due to the large number of files and the need to quickly add new ones.由于文件数量众多且需要快速添加新文件,因此这是一个大问题。

My vague question (sorry) is: can anyone point me toward a better solution?我的模糊问题(对不起)是:谁能指出我更好的解决方案? (A framework better suited to this? A way in Vue to replicate this kind of interactivity with a plain .svg? A way to automate modifying the .svg files to have Vue bindings on their arbitrary numbers/types of svg elements?) (一个更适合这个的框架?在 Vue 中用普通的 .svg 复制这种交互性的一种方法?一种自动修改 .svg 文件以在其任意数量/类型的 svg 元素上绑定 Vue 的方法?)

Thanks--谢谢 -

The easiest way in Vue to add interactivity is to convert them to components Vue 中添加交互性的最简单方法是将它们转换为组件
but this won't work in your situation because the svg's are uploaded by users.但这不适用于您的情况,因为 svg 是由用户上传的。

So you'll need to find or write a component that adds this feature to your app.因此,您需要找到或编写一个组件来将此功能添加到您的应用程序中。

Take a look at https://github.com/seiyable/vue-simple-svg for inspiration.查看https://github.com/seiyable/vue-simple-svg以获得灵感。

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

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