No runtime provider for RuntimeCompiler

◇◆丶佛笑我妖孽 提交于 2019-12-07 09:53:06

问题


I'm trying to follow the accepted answer here, and make a call to RuntimeCompiler.clearCache()

Here's how I've tried to do it:

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'm getting this error:

 ORIGINAL EXCEPTION: No provider for RuntimeCompiler!

What am I missing to here?


回答1:


With RC5+ this providers should be registered on a AppModule level

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

Check this How can I use/create dynamic template to compile dynamic Component with Angular 2.0? for a working plunker




回答2:


Remove the line

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

Then add Compiler to import @angular/core. And replace RuntimeCompiler to 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




回答3:


Add RuntimeCompiler to your component providers. (providers: [RuntimeCompiler], below templateUrl)




回答4:


Add RuntimeCompiler as provider in your 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();
  }   
}


来源:https://stackoverflow.com/questions/39289942/no-runtime-provider-for-runtimecompiler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!