src binding not finding svg file
See original GitHub issueHello there, I’m not sure if this is an issue with the loader or merely a lack of my understanding. It appears that when I attempt to dynamically load an SVG using the v-bind:src
approach on the image tag, the loader cannot find it.
Loading SVG from src path
<img svg-inline :src="`../../static/images/icon-${game.name}.svg`" />
If I write it in normally src="../../static/images/icon-basketball.svg"
it works.
Loading SVG from build path I’ve also tried this:
<img svg-inline :src="`/images/icon-${game.name}.svg`" />
But it ends up just loading up an image tag with the SVG as it’s src instead of inlining the SVG.
Any ideas?
Issue Analytics
- State:
- Created 5 years ago
- Comments:32 (15 by maintainers)
Top Results From Across the Web
Img src for svg file doesn't work if property binding is used in ...
I think if you are receiving data asynchronous then your binding should be work. When the value is received, AsyncPipe signals the component ......
Read more >Using SVG and Vue.js: A complete guide - LogRocket Blog
This is a complete guide to using SVG with Vue, including practical examples and best practices to help you along the way.
Read more >Property binding - Angular
To bind the src property of an <img> element to a component's property, place src in square brackets followed by an equal sign...
Read more >How to Embed, Inline, and Reference SVG Files in Svelte
A guide on using SVG files with Svelte, and how to embed, inline-directly, or reference them, with or without rollup plugins.
Read more >xlink:href - SVG: Scalable Vector Graphics - MDN Web Docs
The target element must be part of the current SVG document fragment. ... If the xlink:href attribute is not provided, the target element ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hello,
I will try to explain it one more time as simple as possible.
This Webpack loader only parses static files. That means, it takes all your
*.vue
files and tries to locate all*.svg
sources in image tags in those files. It doesn’t perform any Vue specific logic, because that happens later in browser (or in SSR process).As for using
@
alias, that’s Webpack stuff. It has nothing to do with Vue.If you have any more questions, feel free to ask.
Thanks
Hello,
while it is true, that majority of use cases are based on displaying svg image is based on dynamic rules, Webpack runs before any Vue logic is able to execute (excluding SSR, which is minority of use cases). Therefore there is no simple way to know, which svg files should Webpack include into bundle. You can read my last message in this issue for more details and possible solutions.
I’m currently working on Vue plugin, which will target this issue, of course with cost of performance.
For your concerns about usefulness of this loader, currently you have more approaches you can use to inline your svg files:
require
function together withv-html
directive, which will increase your build time and bundle size (demo here, read readme first).At time I was writing this plugin, I had to solve situation, where I had to show one of 3 possible svg images about 100+ times (was user configurable). So at that time, it was pretty straightforward to me to do
v-if
,v-else-if
andv-else
together with inline sprites. Time showed me, that people want use this loader different way I designed it. Therefore I’m working on Vue plugin version of this loader as I stated above.If you have any more questions, feel free to ask.
Thanks