2016-09-23 107 views
1

所以我已經從RC1升級到Angular2的最終版本。我做了很多調整,但每次我在我的AppComponent上注入RuntimeCompiler時,都會發生此錯誤。Angular2沒有CompileMetadataResolver的供應商

No provider for CompileMetadataResolver

我不知道這裏發生了什麼,並沒有看到有關此問題的網站的答案。任何幫助將不勝感激,無論如何這裏是我的AppComponent供參考。

import { Component } from '@angular/core'; 
import { Location } from '@angular/common'; 
import { Router } from '@angular/router'; 
import { RuntimeCompiler } from '@angular/compiler'; 

@Component({ 
    selector: 'content', 
    template: '<router-outlet></router-outlet>' 
}) 
export class AppComponent { 
    constructor(
     private location: Location, 
     private router: Router, 
     private runtimeCompiler: RuntimeCompiler 
    ) {; 

     if (localStorage.getItem('id_token') == undefined) { 
      if (location.path().toLowerCase().startsWith('/login')) { 
       // LET IT BE 
      } else { 
       router.navigate(['login']); 
      } 

      runtimeCompiler.clearCache(); 
     } else { 
      router.navigate(['menu']); 
     } 
    } 
} 

在此先感謝您。

回答

2

我會說,你應該添加設置爲應用程序模塊的供應商:

import { COMPILER_PROVIDERS } from "@angular/compiler"; 
... 

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

此設置提供商COMPILER_PROVIDERS包含了所有由RuntimeCompiler需要什麼。有一個example工作plunker使用那段代碼How can I use/create dynamic template to compile dynamic Component with Angular 2.0?和一些更多的細節...

+1

嗨@Radim,這樣做!我繼續在導入時添加RuntimeCompiler,並想知道爲什麼錯誤仍然存​​在,我對這個版本仍然是新的,我想我仍然不明白這個整個NgModule的東西,無論如何,非常感謝的人,節省了我一些時間! – arvstracthoughts