In vue single file component.I import a svg file like this: import A from 'a.svg'
And then how can I use A in my component?
Loading a standard HTML SVG At the end of the day, Vue uses dynamic HTML templating, meaning the most straightforward way to use SVG with Vue is exactly how you might do it with standard HTML: By placing the <svg> inline. By using an <img> tag that links to an external SVG file with the src attribute.
To import and use image in a Vue. js single file component, we can set the src prop of the image to the image path returned by the require function. to set the src prop of the image to the path returned by require . We call require with the relative path of the image to return the resolved path of the image.
There are two ways to import a JavaScript library to the Vue Component. The first is to import a local JavaScript library. Here, you can import the JavaScript library by using the 'import' keyword inside the script tag of your Vue file. import * as mykey from '../assets/js/mykey.
Based on the information you provided, what you can do is:
npm install --save-dev vue-svg-loader
module: { rules: [ { test: /\.svg$/, loader: 'vue-svg-loader', // `vue-svg` for webpack 1.x }, ], },
<template> <nav id="menu"> <a href="..."> <SomeIcon class="icon" /> Some page </a> </nav> </template> <script> import SomeIcon from './assets/some-icon.svg'; export default { name: 'menu', components: { SomeIcon, }, }; </script>
Reference: https://github.com/visualfanatic/vue-svg-loader
I've gotten the following to work in Vue 3. Doesn't require messing with webpack or installing any third party plugins.
<template> <img :src="mySVG" /> </template> <script> export default { name: 'App', data(){ return { mySVG: require('./assets/my-svg-file.svg') } } } </script>
Note: I'm aware that you cannot modify certain pieces of the SVG when using it in img src, but if you simply want to use SVG files like you would any other image, this seems to be a quick and easy solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With