In front-end development, Vue component development often uses SVG icons, but sometimes when we use SVG in applications, there will be problems with it not being displayed.
There are many possible reasons for such a problem. Below we analyze possible problems and solutions.
First, check whether the path is incorrect. Sometimes, SVG icons don't load correctly due to wrong file paths. In this case, you can view the browser's 404 error message by checking the developer tools or console.
If you find an SVG path error, you can solve it by modifying the path or using an absolute path.
When the SVG icon size is set to 0, or the SVG icon is set to hidden, the SVG icon will not be displayed.
Therefore, to ensure that the SVG icon size is appropriate and displayed correctly, you can set the SVG icon default size through the following code:
svg { width: 1em; height: 1em; }
Sometimes, When we use SVG icons in applications, we will find that although the SVG icon is displayed, the fill color is wrong. This problem is usually caused by problems with SVG default colors or Vue component styles.
When dealing with this kind of problem, you can solve it by using the fill attribute. If you are using a Vue component, you can add a style tag to the component to override the SVG default color.
<template> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" class="icon"> <path fill="currentColor" d="M6.7 16.7L15.3 8 6.7 0 5 1.7l6.7 6.6-6.7 6.6z"/> </svg> </template> <style scoped> svg { fill: red; } </style>
In a Vue application, if you are using an SVG component, you need to introduce this component in every place where this component is used.
You can use import
to import SVG components globally or locally. If you are using vue-svgicon, please add the following configuration to ensure that the SVG component is imported correctly:
import Vue from 'vue'; import SvgIcon from 'vue-svgicon'; Vue.component('svg-icon', SvgIcon);
If you still cannot display SVG, please make sure that you import and use it correctly, and check whether there are the following errors in the console:
error: 'svgicon' is not defined
The above solutions should solve most of the problems when using Vue. SVG not displaying issue. If your problem still cannot be resolved, you can seek other solutions by:
In short, if you encounter problems when using SVG icons in Vue applications, you can try the above solutions, or check whether other components in the Vue application affect the display of SVG icons.
The above is the detailed content of vue's svg does not display. For more information, please follow other related articles on the PHP Chinese website!