Missing RuntimeCompiler Provider

I am trying to fulfill the accepted answer here and make a call to RuntimeCompiler.clearCache ()

Here is how I tried to do this:

import { Component } from '@angular/core';
import { OnInit } from '@angular/core';
import { RuntimeCompiler } from '@angular/compiler';

@Component({
    moduleId: module.id,
    selector: 'my-app',
    templateUrl: 'app.component.html',
})

export class AppComponent implements OnInit {
    constructor(private _runtimeCompiler: RuntimeCompiler) {}

    ngOnInit() {
        this._runtimeCompiler.clearCache();
    }   
}

But I get this error:

 ORIGINAL EXCEPTION: No provider for RuntimeCompiler!

What am I missing here?

+4
source share
4 answers

With RC5 +, these providers must be registered at the AppModule level.

@NgModule({
    imports: [
        BrowserModule
        ...
    ],
    declarations: [ ... ],
    bootstrap:    [ ... ],
    providers: [
        COMPILER_PROVIDERS
    ],
})
export class AppModule { }

Check How to use / create a dynamic template to compile a dynamic component with Angular 2.0? for working plunger

+2
source

Delete line

import { RuntimeCompiler } from '@angular/compiler';

Then add a compiler to import @ angular / core. And replace the RuntimeCompiler with the compiler,

import { Component, Compiler } from '@angular/core';
import { OnInit } from '@angular/core';


@Component({
    moduleId: module.id,
    selector: 'my-app',
    templateUrl: 'app.component.html',
})

export class AppComponent implements OnInit {
    constructor(private _compiler: Compiler) {}

    ngOnInit() {
        this._compiler.clearCache();
    }   
}

https://angular.io/docs/ts/latest/api/core/index/Compiler-class.html

+2
source

Add RuntimeCompilerto component suppliers. ( providers: [RuntimeCompiler], below templateUrl)

+1
source

Add RuntimeCompiler as a Provider to component.

import { Component } from '@angular/core';
import { OnInit } from '@angular/core';
import { RuntimeCompiler } from '@angular/compiler';

@Component({
   moduleId: module.id,
   selector: 'my-app',
   templateUrl: 'app.component.html',
   providers: [RuntimeCompiler]
})

export class AppComponent implements OnInit {

  constructor(private _runtimeCompiler: RuntimeCompiler) {}

  ngOnInit() {
      this._runtimeCompiler.clearCache();
  }   
}
+1
source

Source: https://habr.com/ru/post/1653425/


All Articles