1

I have a simple library with a component and I need to use DomSanitizer for html strings. I am consuming this library inside my Application project.

I already know that I should import Domsanitizer and inject it in the constructor as many have already explained: Correct way Provide DomSanitizer to Component with Angular 2 RC6

EDIT:

As you can also check here:

LibraryComponent.ts

import {DomSanitizer, SafeHtml} from '@angular/platform-browser';


export class SqvLibComponent implements OnInit {

  constructor( private sanitizer: DomSanitizer ) {
  }

  sequenceRegion: SafeHtml;

  this.sequenceRegion = this.sanitizer.bypassSecurityTrustHtml(myStringtoSanitize);
}

But I get an error stating:

"No provider for Domsanitizer!"

Does someone understand why? Thank you!

mb925
  • 137
  • 14

1 Answers1

0

As mentioned in this answer in the linked question, you need to ensure that the BrowserModule is imported, otherwise the DomSanitizer wont be provided.

One important thing is that the BrowserModule should be imported by an application root module, not a library module.

In most cases, the BrowserModule wil be correctly be imported, as this is done by default in apps created by the cli. But for the cases that its not, you could add the following checks to your code:

import {Optional}from '@angular/core';

export class SqvLibComponent implements OnInit {
  sequenceRegion: SafeHtml;
  constructor(@Optional() private sanitizer: DomSanitizer ) {
    if(!!this.sanitizer){
      this.sequenceRegion = this.sanitizer.bypassSecurityTrustHtml(myStringtoSanitize); 
    }else{
      // Depending on your component´s ability to correctly work
      // without this dependency you can either warn or raise an error
      console.warn("Ensure that the `BrowserModule` is imported in the application´s root module");
    }
  }
}
Jota.Toledo
  • 27,293
  • 11
  • 59
  • 73
  • Yes I'm importing BrowserModule only in my app root module and not in the library, but the error (or the warn) is shown anyway... – mb925 Jul 10 '19 at 08:40
  • @mb925 hmm, any chance for you to provide a small reproduction of the issue in github? Could you also add the angular and cli versions used? was the library created with the cli? how is it being imported into the consumer app? – Jota.Toledo Jul 10 '19 at 08:54
  • Yes, the library was created with the CLI. Angular version: 7.2.15. Angular-cli: 7.3.9. I try to reproduce the problem here on gitHub, if I can then I post a link. – mb925 Jul 10 '19 at 09:05
  • OK here you find the code of my main app, that you can use in any classic angular project: https://github.com/mb925/libSqv/tree/master/app and you can install my library with: npm i sqv-library – mb925 Jul 10 '19 at 09:20
  • I would be more interested in the lib code as its most probably causing the issue. Even better would be a minimal setup of your application and library together. From that code the one line that calls my attention is `import { SqvLibModule } from 'node_modules/sqv-library/dist/sqv-lib'`; is this the same package that you are using in your problematic app? or did you released it in NPM for the purpose of this question? – Jota.Toledo Jul 10 '19 at 09:26
  • here is my library code:https://github.com/mb925/libraryCodeSqv I released the package for the question, actually in my app I'm using it via npm link. I don't know how to setup the projects together for a demonstration, I have to check how to do.. I'm a bit new to this. – mb925 Jul 10 '19 at 09:40
  • The easiest way would be to create a new folder, copy the package.json/package-lock.json files and then the required stuff for the issue into it. My guess is that either the use of npm link (generally speaking a wrong linking of the lib) or a wrong setting of `tsconfig.app.json`/`tsconfig.lib.json` files is causing the issue. Did you changed any of those files?. Anyhow, for anyone to look into your issue, a minimal working repro is necessary. – Jota.Toledo Jul 10 '19 at 10:14
  • I had to change tsconfig.json module and target to 'esnext' and 'es2018' respectively, but I didn't touch tsconfig.lib.json. I think npm link is correct, because without the Domsanitizer everything is working fine (even though of course the html gets displayed in a weird way because is not sanitized). Anyway I'll try to make the folder and then let you know. – mb925 Jul 10 '19 at 10:32
  • @mb925 Im almost sure that the `tsconfig.lib.json` file generated by the CLI extends `tsconfig.json`. In that case, your changes on `tsconfig.json` would have an effect on the config file for the lib. – Jota.Toledo Jul 10 '19 at 11:59
  • 1
    At the end it was a problem with npm link. I don't understand because without the Domsanitizer npm link was working correctly, and after importing DomSanitizer it became broken. I am now using the library installing it from npm, even though this slows a bit the testing phase – mb925 Jul 10 '19 at 14:53