【问题标题】:Angular2 No provider for CompileMetadataResolverAngular2没有CompileMetadataResolver的提供者
【发布时间】:2017-01-31 22:09:23
【问题描述】:

所以我已经从 RC1 升级到了 Angular2 的最终版本。我做了很多调整,但是每次我在我的AppComponent 上注入RuntimeCompiler 时,都会出现这个错误。

我不知道这里发生了什么,也没有看到有关此问题的网络答案。任何帮助将不胜感激,无论如何这里是我的 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']);
        }
    }
}

提前谢谢你。

【问题讨论】:

    标签: angular angular2-directives angular2-compiler


    【解决方案1】:

    我会说,您应该将提供程序集添加到应用模块:

    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? 和一些更多细节......

    【讨论】:

    • 嗨@Radim,做到了!我一直在导入时添加 RuntimeCompiler 并想知道为什么仍然会出现错误,我还是这个版本的新手,我想我仍然没有理解整个 NgModule 的东西,无论如何,非常感谢,帮我节省一些时间!跨度>
    猜你喜欢
    • 2016-04-06
    • 2016-12-18
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 2016-12-20
    • 1970-01-01
    • 2016-05-28
    相关资源
    最近更新 更多