2016-02-19 75 views
7

“的元素没有成分的指令”我不能让角的DynamicComponentLoader在beta.6工作DynamicComponentLoader在Angular2测试:

我已经采取了动态元件装配from their docs示例代码,并将其添加到自己的工作起动码为plnkr, see here

下面的代码:

import {Component, DynamicComponentLoader, ElementRef} from 'angular2/core'; 


@Component({ 
    selector: 'child-component', 
    template: 'Child' 
}) 
class ChildComponent { 
} 


@Component({ 
    selector: 'my-app', 
    template: '<h1>My First Angular 2 App</h1><div #child></div>' 
}) 
export class AppComponent { 
    constructor(dcl: DynamicComponentLoader, elementRef: ElementRef) { 
    dcl.loadIntoLocation(ChildComponent, elementRef, 'child'); 
    } 
} 

这里是堆栈跟踪,它说: “有一个在单元没有成分的指令”:

EXCEPTION: Error during instantiation of AppComponent!.BrowserDomAdapter.logError @ angular2.dev.js:23083 
angular2.dev.js:23083 ORIGINAL EXCEPTION: There is no component directive at element [object Object]BrowserDomAdapter.logError @ angular2.dev.js:23083 
angular2.dev.js:23083 ORIGINAL STACKTRACE:BrowserDomAdapter.logError @ angular2.dev.js:23083 
angular2.dev.js:23083 Error: There is no component directive at element [object Object] 
    at new BaseException (angular2.dev.js:7351) 
    at AppViewManager_.getNamedElementInComponentView (angular2.dev.js:6441) 
    at DynamicComponentLoader_.loadIntoLocation (angular2.dev.js:12375) 
    at new AppComponent (run.plnkr.co/mk31ybnwEtsiX31r/app/app.component.ts!transpiled:33) 
    at angular2.dev.js:1420 
    at Injector._instantiate (angular2.dev.js:11459) 
    at Injector._instantiateProvider (angular2.dev.js:11395) 
    at Injector._new (angular2.dev.js:11385) 
    at InjectorInlineStrategy.instantiateProvider (angular2.dev.js:11149) 
    at ElementDirectiveInlineStrategy.init (angular2.dev.js:9081)BrowserDomAdapter.logError @ angular2.dev.js:23083 
angular2.dev.js:23083 ERROR CONTEXT:BrowserDomAdapter.logError @ angular2.dev.js:23083 
angular2.dev.js:23083 _ContextcomponentElement: nullelement: my-appinjector: Injector__proto__: _ContextBrowserDomAdapter.logError @ angular2.dev.js:23083 
angular2-polyfills.js:1152 DEPRECATION WARNING: 'dequeueTask' is no longer supported and will be removed in next major release. Use removeTask/removeRepeatingTask/removeMicroTask 

回答

16

更新4(最后)

DynamicComponentLoader正被弃用(请参阅commit)。现在正确的方法是在构造函数中使用ViewContainerRefComponentResolver(请参见下面的更新3)。



在beta.1有重大更改。您不能再访问构造函数中的视图。所以你可以将这个例子移动到ngOnInit(),它会起作用。

changelog

组件视图引述当组分构造函数被调用还没有被创建。 - >使用OnInit的生命周期回调访问组件的

export class AppComponent { 
    constructor(private dcl: DynamicComponentLoader, private elementRef: ElementRef) { 

    } 
    ngOnInit() { 
    this.dcl.loadIntoLocation(ChildComponent, this.elementRef, 'child'); 
    } 
} 

入住这plnkr你的榜样工作的看法。

更新

只为你的信息我已经发出了PR(见#7186)固定在源代码中的例子。所以希望他们会审查它,它会通过。

更新2

由于beta.16 loadIntoLocation除去,错误不再重复的。 loadNextToLocation现在需要ViewContainerRef。我打开了一个新的PR(请参阅#8241)以添加它的一个新示例,其他所有内容已被原始PR覆盖。

代码示例(更新3)

如上所述现在不用loadIntoLocation但相同的行为通过使用ViewContainerRefComponentResolver实现。

constructor(cmpResolver: ComponentResolver, viewContainer: ViewContainerRef) { 

    cmpResolver.resolveComponent(MyComponent) 
    .then((factory: ComponentFactory) => { 
     viewContainer.createComponent(factory, 0, viewContainer.injector) 
    }); 
} 

参考

关于loadNextToLocation,有两种方法,都使用ViewContainerRef

  • 使用ViewContainerRef从元件本身
constructor(private dcl: DynamicComponentLoader, viewContainer: ViewContainerRef) { 
    dcl.loadNextToLocation(MyComponent, viewContainer).then(ref => {}); 
} 
  • 上视图
// Assume the next view 
template : '<div #container></div>'; 

class MyClass { 
    @ViewChild('container', {read: ViewContainerRef}) container : ViewContainerRef; 

    constructor(private dcl: DynamicComponentLoader) {} 

    ngAfterViewInit() { 
     this.dcl.loadNextToLocation(MyDynamicCmp, this.container).then(ref => {}); 
    } 
} 

plnkr与上面的例子中使用一些元件。

+0

非常好,谢谢! – Hoff

+0

您是否介意更新您的示例代码以反映API更改? – bodine

+1

@bodine完成! –