When I updated angular2 from rc4 to rc5, the previous dynamically created components did not work properly. The reason may be that a service was used in the dynamically created components, and this service could not be obtained, but no specific solution was found. Can anyone help, the following is the code
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