如何解决绕过循环依赖
如果组件使用服务并且服务初始化组件,是否有更好的方法来解决循环依赖? 我这里所做的是,由于组件是由服务初始化的,所以我将服务本身注入到组件中,而没有使用角度依赖注入。
工作区服务
@Injectable()
export class WorkspaceService {
name = "John";
constructor(
private cfr: ComponentFactoryResolver,private appRef: ApplicationRef,private injector: Injector
) {
this.appendWorkspacetoBody();
}
private appendWorkspacetoBody(): void {
const workspaceRef = this.cfr
.resolveComponentFactory(WorkspaceComponent)
.create(this.injector);
workspaceRef.instance.workspaceService = this;
this.appRef.attachView(workspaceRef.hostView);
const domElem: HTMLElement = (workspaceRef.hostView as EmbeddedViewRef<any>)
.rootNodes[0] as HTMLElement;
document.body.appendChild(domElem);
}
}
工作区组件
@Component({
selector: "app-workspace",template: "{{workspaceService.name}}"
})
export class WorkspaceComponent implements OnInit {
public workspaceService: WorkspaceService
ngOnInit() {}
}
要检查问题代码,请转到stackblitz
解决方法
我为这个问题找到的更好的解决方案是为组件初始化编写一个单独的服务,该服务通过在 APP_INITIALISER 中提供从模块执行。
模块
// @dynamic
@NgModule({
imports: [BrowserModule,FormsModule],declarations: [AppComponent,HelloComponent,WorkspaceComponent],bootstrap: [AppComponent],providers: [
WorkspaceService,WorkspaceInitializeService,{
provide: APP_INITIALIZER,useFactory: (service: WorkspaceInitializeService) => () =>
service.appendWorkspaceToBody(),multi: true,deps: [WorkspaceInitializeService]
}
]
})
export class AppModule {}
服务
@Injectable()
export class WorkspaceService {
name = "John";
constructor() {
}
}
初始化服务
@Injectable()
export class WorkspaceInitializeService {
constructor(
private cfr: ComponentFactoryResolver,private injector: Injector
) {}
appendWorkspaceToBody(): void {
const workspaceRef = this.cfr
.resolveComponentFactory(WorkspaceComponent)
.create(this.injector);
this.injector.get(ApplicationRef).attachView(workspaceRef.hostView);
const domElem: HTMLElement = (workspaceRef.hostView as EmbeddedViewRef<any>)
.rootNodes[0] as HTMLElement;
document.body.appendChild(domElem);
}
}
要查看解决方案代码,请转到stackblitz
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。