2016-08-08 86 views
1

所以我搜索谷歌找不到任何關於這個例外。Angular2沒有提供ViewUtils

platform-browser.umd.js:1900 EXCEPTION: Error: Uncaught (in promise): No provider for ViewUtils! 

我所試圖做的是創建和動態插入組件,並與對象注入它:

所以我的基類,做的創建看起來是這樣的:

import { Component, OnInit, AfterViewInit, OnDestroy, ComponentResolver, ViewContainerRef, Injector, ReflectiveInjector, provide} from '@angular/core'; 
... 
import { customNode } from '../my-own-types/backend.types'; 
import { LandingComponent } from '../../pages/landing/landing.component'; 

@Component({ 
    moduleId: module.id, 
    template: '<template #page_content></template>)', 
    providers: [ NodeService ] 
}) 

export class BaseComponent implements OnInit, AfterViewInit, OnDestroy { 
    private error:string | boolean = false; 
    private cmpRef:any; 

    constructor(
     private _injector: Injector, 
     private VCR: ViewContainerRef, 
     private CR: ComponentResolver 
    ) {} 

    ... 

    ngAfterViewInit() { 
     ... //getting node from service 
     if (node) { 
      let resolvedProviders = ReflectiveInjector.resolveAndCreate([ 
        provide(customNode, {useValue: node}) 
       ]); 
      // let child = ReflectiveInjector.fromResolvedProviders(resolvedProviders, this._injector); 
      switch (node.type || '') { 
       case 'landing' : 
        this.CR.resolveComponent(LandingComponent).then((factory) => { 
         this.cmpRef = this.VCR.createComponent(factory, 0, resolvedProviders, []); 
        }); 
       break; 
       default: { 
        console.log("BASE COMPONENT: ERROR!!!!"); 
       } 
      } 
     } 
    } 
    ... 
} 

在我的LandingComponent的構造函數中,我想要這樣做:

constructor(
     private node: customNode, 
     private _injector: Injector) { 

     console.log("Landing page constructed"); 

     this.loadNode = (node) => { 
      this.content = node.content; 
      this.created = node.created; 
     } 
     if (!this.node) 
      this.node = _injector.get("node"); 
     if (this.node) 
      this.loadNode(this.node) 
    } 
+0

您使用的是哪個版本的Angular2? –

+0

@ThierryTemplier 2.0.0-rc.4 – Gerardlamo

回答

0

所以我找到了一種讓它工作的方法,現在已經足夠好了,但我猜測並不是很好的做法。

解決方案:我沒有使用reflectionInjectors左右,只是調用LandingComponent中定義的函數。

在我baseComponent,AfterViewInit我現在有:

ngAfterViewInit() { 
    ... //Code to get node 
    if (node) { 
     switch (node.type || '') { 
      case 'page' : 
       this.CR.resolveComponent(LandingComponent).then((factory) => { 
        this.VCR.clear(); 
        this.cmpRef = this.VCR.createComponent(factory, 0, this._injector, []); 
        this.cmpRef.instance.loadNode(node); 
        return this.cmpRef; 
       }); 
      break; 
      default: { 
       console.log("BASE COMPONENT: ERROR!!!!"); 
      } 
     } 
    } 
} 

我LandingComponent現在看起來像:

constructor(private node: customNode, private _injector: Injector) {} 

loadNode(node) { 
    this.content = node.content; 
    this.created = node.created; 
} 
1

你試圖添加母公司注入?

let resolvedProviders = ReflectiveInjector.resolveAndCreate([ 
       provide(customNode, {useValue: node}) 
      ], this._injector); 
相關問題