angular.js - angular2 rc5 No component factory found for someComponent

查看:219
本文介绍了angular.js - angular2 rc5 No component factory found for someComponent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

当我将angular2 从rc4更新到rc5,之前的动态创建组件无法正常工作,原因可能是因为动态创建的组件中用到一个service,而这个service无法获得,但没有找到具体的解决办法,谁能帮忙看看,下面是代码
I update angular2 to rc5,service can't share when dynamic create SharedComponent,below is error:

NoComponentFactoryError {message: "No component factory found for SharedComponent", stack: "Error: No component factory found for FormComponen…tron/output/electron/build/polyfills.js:16347:22)"}
component
:
SharedComponent(shareService)
arguments
:
[Exception: TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context. at Function.remoteFunction (<anonymous>:3:14)]
caller
:
[Exception: TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context. at Function.remoteFunction (<anonymous>:3:14)]
length
:
1
name
:
"SharedComponent"

below is my code
----SharedComponent---

@Component({
    template: require('./share.component.html'),
    styles: [require('./share.component.css')]
})
export class SharedComponent implements OnInit {
    constructor(@Optional() private shareService:ShareService) { 
        ......
    }
}

---shared.module.ts-------

@NgModule({
  imports: [CommonModule, FormsModule],
  declarations: [SharedComponent],
  exports: [CommonModule, FormsModule, SharedComponent]
})
export class SharedModule {
  static forRoot(): ModuleWithProviders {
    return {
      ngModule: SharedModule
    };
  }

---A.module.ts---------

@NgModule({
  imports: [
    sharedModule.forRoot()
  ],
  declarations: [
    AComponent
  ],
  providers:[FormService]
})
export class AModule { }

-----A.component.ts------

@Component({
  template: require('./a.component.html'),
  styles: [require('./a.component.css')],
  providers: [ShareService]
})
export class UserComponent implements OnInit {
  @ViewChild('body', { read: ViewContainerRef }) bodyContainerRef: ViewContainerRef;
  constructor(private compiler: ComponentFactoryResolver,private shareService: ShareService) {
  }
  ngOnInit() {
    //Dynamic create SharedComponent
    let componentFactory = this.compiler.resolveComponentFactory(SharedComponent);
    this.bodyContainerRef.createComponent(componentFactory);
  }
}

解决方案

A.module.ts

@NgModule({
  imports: [
    sharedModule.forRoot()
  ],
  declarations: [
    AComponent
  ],
  entryComponents: [
    SharedComponent
  ]
  providers:[FormService]
})
export class AModule { }

这篇关于angular.js - angular2 rc5 No component factory found for someComponent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆