Home  >  Article  >  Web Front-end  >  vue's svg does not display

vue's svg does not display

WBOY
WBOYOriginal
2023-05-11 11:42:374151browse

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.

Problem 1: SVG path error

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.

Issue 2: SVG size error

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;
}

Issue 3: SVG fill color is wrong

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>

Problem 4: SVG component is not introduced correctly

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:

  1. Check whether the SVG icon is damaged.
  2. Check whether the browser supports SVG format.
  3. Restart the application or browser.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn