Issue with Vue SVG component failing to render on the DOM

I am currently working on a component that loads an SVG when utilized. However, I have encountered an issue where it does not load in the DOM. Upon inspection of the element, it always displays as shadow-root (closed).

Interestingly, it does show up when embedded in an image. The reason I prefer using SVG over images is because I want the ability to change the color dynamically.

Below is how the inspected element appears:

<svg aria-hidden="true">
    <use xlink:href="/img/apartment.908eb4d5.svg#apartment">
        #shadow -root (closed)
    </use>
</svg>

Usage

<SvgIcon icon-name="apartment" />

Component

<template>
  <svg aria-hidden="true">
    <use :xlink:href="require(`@/icons/${iconName}.svg`)+ `#${iconName}`"></use>
  </svg>
</template>

<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  name: "SvgIcon",
  props: {
    iconName: {
      type: String,
      default: "book"
    }
  }
});
</script>

SVG

<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20">
<path fill="#000000" d="M14 6h1v1h-1v-1z"></path>
...
</path>
</svg>

Answer №1

The <use> tag duplicates the specified node using the href attribute. Ensure that the SVG file you are loading contains the correct id attribute when utilizing a fragment link.

<svg id="house" xmlns="http://www.w3.org/2000/svg" width="20" height="20">
    <path fill="#000000" d="M14 6h1v1h-1v-1z"></path>
    ...
</svg>

Additionally, usage of xlink:href is no longer recommended.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm getting a "module not found" error - what's the solution?

const { getIo } = require('services/socketio'); const restful = require('utils/restful'); const publicApiService = require('services/publicApi'); const accessTokenMiddleware = require('middleware/accessToken'); const ...

Iterate through Javascript quotes and automatically navigate to the first quote once the loop is completed

I've encountered an issue with the script where I am unable to loop back to the first element once all child elements have been traversed. https://jsfiddle.net/pad5bp0o/2/ Here is the jQuery code snippet: $(document).ready(function() { var length ...

the current context within the child event handler of a functional component

I am struggling to come up with custom event handlers for child components/elements within a functional component. The issue arises when utilizing a render() function to create the child components, as I am unable to access their this context. Let's ...

Unlock the potential of Google Charts in Android Chrome Browser Webview

I recently completed a project where I developed a web-based login interface and integrated various charts using AngularJS and the Google Charts API. To my dismay, everything works perfectly fine on my laptop's Chrome browser, but when I try to access ...

Passing props dynamically with TypeScript ENUMs

As I begin learning TypeScript with React, I am creating practice scenarios to enhance my understanding along the way. export enum Colors { Blue = "#0000FF", Red= "#FF0000", Green = "#00FF00", } export const ColorComponent: React.FC<props> = ...

Exploring the ‘ref’ feature in React alongside Material-UI

In my attempt to access input data through React's "ref" attribute on a TextField in Material-UI, I've encountered some difficulties. It doesn't seem straightforward to achieve this using the 'inputRef' or 'inputProps' me ...

A step-by-step guide on incorporating the Danfo.js NPM package into a Vue 3 project

I'm facing a challenge that seems basic, yet I can't quite wrap my head around it. How do you import and utilize a third-party JavaScript library in vue 3? Specifically, I am attempting to incorporate Danfo.js by running npm install danfojs (eve ...

Having difficulty with utilizing array.every() properly, leading to inaccurate results

Struggling to validate an array of IDs using a custom validator in a nestjs project. The issue arises when passing the array of IDs to a service class for database querying, as the validation always returns true even with incorrect IDs. Snippet of the cus ...

Choose specific elements based on their attribute within a class, and then either apply or remove a class

I have a project where I need to store the data information of each element in my navigation menu when a button is clicked. This project is a one-page website with two levels of navigation: Main menu Button at the bottom of each "page" Currently, I hav ...

Is there a way for me to obtain the present value of the chosen button in the list below?

I am working with a group of three buttons labeled English, Hindi, and Urdu. How can I retrieve the value of the selected button in a JavaScript function? <div class="btn-group" data-toggle="buttons"> <label class="btn btn-primary active"> ...

Encountering an issue with a Discord bot causing it to malfunction and deviate from its intended

Initially, everything appears to be functioning properly with the bot. When I execute the ?listen command, it responds correctly with bot is collecting messages now.... However, the ?stop command does not seem to have any effect. Furthermore, when I try th ...

Button react-native press following textInput within scroll view aware of keyboard movements

I'm currently facing an issue where I have a TextInput and a button nested inside a KeyboardAwareScrollView. The goal is for the user to input some text and then tap the button created using TouchableOpacity, which should send the inputted text forwar ...

How can I extract the file name from a FileStreamResult when making a request with Ajax or Axios?

Currently, in my asp.net core api controller, I have the following code snippet: return new FileStreamResult(excel, "application/ms-excel") { FileDownloadName = filename }; Afterwards, I make an ajax request to fetch this fil ...

Determine the presence of a particular set of values within an array of named objects stored in the browser's localStorage using JavaScript

Previously, I stored various objects in localStorage in the following format: console.log(localStorage) // The storage output may vary, with different objects (such as randid) Storage { 876346545: "{"ABA":"876346545","A ...

Implementing Yii pagination with asynchronous loading

Can anyone help me enable pagination using Ajax in my code? I have a Controller that updates content via Ajax. function actionIndex(){ $dataProvider=new CActiveDataProvider('News', array( 'pagination'=>array( ...

Is it possible to assign multiple attributes using the setAttribute function of the DOM?

Imagine I'm looking to streamline the process of creating an input element through the DOM. Rather than the conventional method shown below var input = document.createElement("input"); input.setAttribute("class", "my-class"); input.setAttribute("type ...

Content is pushed by a scrolling drop down menu

I had a drop-down menu that was functioning well. However, when I made changes to allow scrolling for too many items, it started behaving differently. The drop-down without scrolling doesn't move down, but the one with scrolling does. JS var maxHei ...

Tips for displaying the preceding div of a clicked div using Angular.js

I have an HTML structure that looks like this: <div class="main" ng-controller="firstcontroller"> <div class="submain"> <div class="first" ng-show="display">displayed</div> <div class="second" my- ...

How can I dynamically update an input field within a Bootstrap modal only if the field is not already filled?

I am facing an issue with my dynamic table form. After filling out the form, I want the values from all fields to be inserted into one field with commas separating them. However, I am unsure of how to check if a field is empty and insert data, or update da ...

Leveraging real-time geographical location data for a weather widget with OpenWeatherAPI integration

My goal is to incorporate the current geolocation feature into a weather widget that I am developing. At the moment, I can only show data from cities based on an external source. My coding knowledge is quite limited. I am not a professional in this field, ...