Error loading personalized SVG on mat-icon

I have been attempting to load a custom SVG using the MatIconRegistry within my component. Here is the code snippet I am trying:

  constructor(fb: FormBuilder,
    private matIconRegistry: MatIconRegistry,
    private domSanitizer: DomSanitizer) {
    this.matIconRegistry.addSvgIconInNamespace(
      'capp',
      'reassign',
      this.domSanitizer.bypassSecurityTrustResourceUrl('../../../../assets/images/reassign.svg')
    );
    this.form = fb.group({
      query: ''
    });
  }

When I use the icon in the browser like so:

<mat-icon svgIcon="capp:reassign"></mat-icon>
, an error is displayed in the console:

Error retrieving icon: Failed to construct 'URL': Invalid URL

I have experimented with different relative paths for the SVG file, such as ./, /, and relative to the route (../../). Despite this, the only way I have achieved success is by specifying the full URL. However, this approach may not be consistent when deployed, and I believe the path to the SVG should be relative.

If anyone has any suggestions or advice on how to resolve this issue, I would greatly appreciate it.

Answer №1

In order for it to function properly, I have found that specifying the complete URL is necessary. However, this may pose a problem once deployed.

The solution is straightforward: it requires an absolute URL rather than a relative one.

To address the issue of potential differences when deployed, you can utilize window.location.origin. This method will work seamlessly in any environment as long as the application's base URL remains as /.

matIconRegistry.addSvgIconInNamespace(
  'capp',
  'reassign',
  domSanitizer.bypassSecurityTrustResourceUrl(window.location.origin + '/assets/images/reassign.svg')
);

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

Challenges when building a production version of an Expo app with Typescript

Attempting to perform a local production build, I ran expo start --no-dev --minify. Only the initial template file displays, stating "Open up App.tsx to start working on your app," and none of my work is visible... Has anyone else encountered this issue a ...

Ways to conduct testing on React Native Typescript app COMPONENTS using jest

I've been struggling to set up testing for my React Native Typescript Components using Jest. Despite searching through various examples and solutions (such as this one, that one, another link, etc.), I still can't seem to get it working. Even fol ...

What is the process for incorporating a manually created Angular package into a project as a node modules package?

Looking for help on integrating a custom-built package into an Angular project instead of the default one installed via npm. I attempted to replace the original package in node_modules with my custom build, but encountered issues. The specific package I& ...

Having trouble upgrading from Ionic v5 to v6?

I have an existing Ionic 5 project that I am looking to update to V6. Following the official documentation guide, I attempted to update Angular to the latest version but encountered the following error: C:\Users\Documents\WRepo>npx @angul ...

Combining enums and functions

I have found the concept of combining enums with namespaces to be incredibly valuable. For instance, consider the following: enum Status : { OK = 1, NOT_OK = 2, } namespace Status { function Color(status : Status) { if(status == Sta ...

Challenge encountered with asynchronous angular queries

Dealing with asynchronous calls in Angular can be tricky. One common issue is getting an array as undefined due to the asynchronous nature of the calls. How can this be solved? private fetchData(id){ var array = []; this.httpClient.get('someUrl ...

Hermes, the js engine, encountered an issue where it was unable to access the property 'navigate' as it was undefined, resulting

Whenever I switch from the initial screen to the language selection screen, I encounter this error and have exhausted all possible solutions. I attempted to utilize "useNavigation" but still encountered errors, so I resorted to using this method instead. T ...

Encountering a 404 Error while attempting to access a locale in Angular 5

Currently, I am facing an issue while trying to load a locale in my Angular 5 project that is being run from within VS 2015. According to the documentation, to add a locale, I should include the following code in the app.module: import { NgModule, LOCALE_ ...

Managing API responses using Redux and Typescript

As a beginner in Typescript, I am struggling to integrate Redux with it. The documentation on using Redux with Typescript is confusing me. I am attempting to fetch data and dispatch it to my reducer for future use, just as I did before adopting Typescript ...

Unable to locate the 'socket.io' module in a TypeScript project on Visual Studio using node.js

Currently working on a node.js project within Visual Studio. Everything was running smoothly until I attempted to import 'socket.io' and encountered the error message – Cannot find module 'socket.io'. Despite having it installed in th ...

Having trouble triggering a Bootstrap 5 modal using TypeScript

Have you heard the news that Bootstrap 5 has been released? To open a modal using JavaScript, the official website provides the following code: var myModal = new bootstrap.Modal(document.getElementById('myModal'), options) myModal.toggle() Howev ...

Using TypeScript to implement Angular Draggable functionality within an ng-template

Sorry if this question has been asked before, but I couldn't find any information. I am trying to create a Bootstrap Modal popup with a form inside and I want it to be draggable. I have tried using a simple button to display an ng-template on click, b ...

Unable to modify data with ionic and firebase in child node format

I am encountering an issue when updating data with Ionic Firebase using the following code. Instead of rewriting the previous data, it simply creates new data entries. Here is the code snippet: updateLaporan() { this.id =this.fire.auth.cur ...

I encountered an issue while generating a crypto address on the Waves blockchain using the @waves/waves-crypto library in TypeScript

Encountering an issue with finding "crypto-js" in "@waves/waves-crypto". Despite attempts to uninstall and reinstall the module via npm and importing it using "*wavesCrypto", the error persists within the module's index.d.ts file. I am attempting to ...

Using Typescript to define property types based on their respective values

Is it possible to set a property type based on the property value? For example, if the command is: If 'set' then the payload must be PayloadSet If 'put' then the payload must be PayloadPut If 'del' then the payload must be ...

Spring Boot app experiences issues with certain endpoints not functioning properly upon receiving requests from the Angular front end

My code includes the following endpoint: @PutMapping(path = "/like/{id}") public void likeQuestion(@PathVariable("id") String id, @Valid @NotNull @RequestBody Student student) { logger.info(id, student); questionService.likeQues ...

Steps for exporting various elements from a .vue file

In my Vue project, I am incorporating TypeScript along with Vue. There is a specific scenario where I need to export multiple items from my .vue file. Here's an example of what I want to achieve: // FooBar.vue <template> ... </template& ...

Using .on('mouseover', d => ..) does not yield the same `d` value as using .attr('foo', d => ..) in D3.js

I'm facing an issue with a mouseover tooltip in Observable that seems to fail when I transfer it to a Grafana plugin using React, D3, and Typescript. The technique I followed can be found in this article: Link to Article To simplify the code and deb ...

Running a Vue.js 3 application with TypeScript and Vite using docker: A step-by-step guide

I am currently facing challenges dockerizing a Vue.js 3 application using Vite and TypeScript. Below is my Dockerfile: FROM node:18.12.1-alpine3.16 AS build-stage WORKDIR /app COPY package.json ./ RUN yarn install COPY . . RUN yarn build-only FROM ngin ...

There was an issue detected in the ./node_modules/@angular/core/esm5/core.js file: Module cannot be located due to TypeError indicating that dep.isEqualResource is

I recently updated the package.json in my Angular Project using the command ncu -u. However, after the update, I encountered the following error when running ng serve: ERROR in ./node_modules/@angular/core/esm5/core.js Module not found: TypeError: dep.isE ...