本文介绍了DynamicComponentLoader在Angular2测试:"在元件QUOT没有组件指令;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不能让角的DynamicComponentLoader在beta.6工作
我已经采取了例如code动态元件装载从他们的文档,并把它添加到他们的工作启动code中的。
这里的code:
进口{组件,DynamicComponentLoader,ElementRef}从'angular2 /核心;
@零件({
选择:儿童分量',
模板:'儿童'
})
类ChildComponent {
}
@零件({
选择:我的应用,
模板:'< H1>我的第一角2应用< / H1>< DIV#儿童及GT;< / DIV>'
})
出口类AppComponent {
构造器(DCL:DynamicComponentLoader,elementRef:ElementRef){
dcl.loadIntoLocation(ChildComponent,elementRef,'孩子');
}
}
这里是堆栈跟踪,它说:有是没有的元素组成指令
异常:价格AppComponent实例化过程中的错误.BrowserDomAdapter.logError @ angular2.dev.js:23083
angular2.dev.js:23083原始异常:有一个在元素[对象的对象]无组件指令BrowserDomAdapter.logError @ angular2.dev.js:23083
angular2.dev.js:23083 ORIGINAL堆栈跟踪:BrowserDomAdapter.logError @ angular2.dev.js:23083
angular2.dev.js:23083错误:没有组件指令的元素[对象的对象]
在新BaseException(angular2.dev.js:7351)
在AppViewManager_.getNamedElementInComponentView(angular2.dev.js:6441)
在DynamicComponentLoader_.loadIntoLocation(angular2.dev.js:12375)
在新AppComponent(run.plnkr.co/mk31ybnwEtsiX31r/app/app.component.ts!transpiled:33)
在angular2.dev.js:1420
在Injector._instantiate(angular2.dev.js:11459)
在Injector._instantiateProvider(angular2.dev.js:11395)
在Injector._new(angular2.dev.js:11385)
在InjectorInlineStrategy.instantiateProvider(angular2.dev.js:11149)
在ElementDirectiveInlineStrategy.init(angular2.dev.js:9081)BrowserDomAdapter.logError @ angular2.dev.js:23083
angular2.dev.js:23083错误上下文:BrowserDomAdapter.logError @ angular2.dev.js:23083
angular2.dev.js:23083 _ContextcomponentElement:nullelement:我-appinjector:Injector__proto__:_ContextBrowserDomAdapter.logError @ angular2.dev.js:23083
angular2-polyfills.js:1152 DE preCATION警告:'dequeueTask'不再支持,并会在下一主要版本中删除。使用removeTask / removeRepeatingTask / removeMicroTask
解决方案
在beta.1有一个重大更改。你不能在构造函数中再访问该视图。所以,你可以移动的例子ngOnInit(),它会正常工作。
从
export class AppComponent {
constructor(private dcl: DynamicComponentLoader, private elementRef: ElementRef) {
}
ngOnInit() {
this.dcl.loadIntoLocation(ChildComponent, this.elementRef, 'child');
}
}
Check this plnkr with your example working.
Update
Just for your information I've sent a PR (see #7186) to fix the examples in the source code. So hopefully they will review it and it will pass.
这篇关于DynamicComponentLoader在Angular2测试:"在元件QUOT没有组件指令;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!