我有逻辑问题。我需要一个组件,它将被导入到一个服务中,并在这个组件中并行,我需要这个服务。错误如下:
Circular dependency: dist\services\modal.service.js -> dist\components\modal\modal.component.js -> dist\services\modal.service.js
import { ComponentFactoryResolver, EmbeddedViewRef, ApplicationRef, ComponentRef, Injectable, Injector
} from '@angular/core';
import { ModalComponent } from '../components/modal/modal.component';
@Injectable({
providedIn: 'root'
})
export class ModalService {
constructor(
private componentFactoryResolver: ComponentFactoryResolver,
private appRef: ApplicationRef,
private injector: Injector
){}
private data = {};
private last;
open(component: any, obj:any = {}) {
if(!obj.id) obj.id = new Date().getTime();
// Create a component reference from the component
const componentRef = this.componentFactoryResolver
.resolveComponentFactory(component)
.create(this.injector);
// Attach component to the appRef so that it's inside the ng component tree
this.appRef.attachView(componentRef.hostView)
// Get DOM element from component
const contentElem = (componentRef.hostView as EmbeddedViewRef<any>)
.rootNodes[0] as HTMLElement;
// Create a component reference from the component
let componentRefer = this.componentFactoryResolver
.resolveComponentFactory(ModalComponent)
.create(this.injector);
// Attach component to the appRef so that it's inside the ng component tree
this.appRef.attachView(componentRefer.hostView);
// Get DOM element from component
const domElem = (componentRefer.hostView as EmbeddedViewRef<any>)
.rootNodes[0] as HTMLElement;
// Append DOM element to the body
document.body.appendChild(domElem);
// Append DcontentElemOM element to the body
domElem.querySelector("#modalHoster").appendChild(contentElem);
// Wait some time and remove it from the component tree and from the DOM
this.data[obj.id]={
componentRefer: componentRefer,
appRef: this.appRef
}
this.last=obj;
return obj.id;
}
pull(){
return this.last;
}
close(id){
this.data[id].appRef.detachView(this.data[id].componentRefer.hostView);
}
}
组件.ts
import { Component, OnInit } from '@angular/core';
import { ModalService } from '../../services/modal.service';
@Component({
selector: 'modal',
templateUrl: './modal.component.html',
styleUrls: ['./modal.component.scss']
})
export class ModalComponent implements OnInit {
close(){
this.mod.close(this.id);
}
constructor(private mod: ModalService){}
ngOnInit() {
let obj=this.mod.pull();
for(let key in obj){
this[key]=obj[key];
}
}
}
可能是我的逻辑错了,这就是我要问的。这两个服务和组件在模块内部,而不是应用程序。应用程序仅使用服务,组件不可访问。服务需要一段html/css/ts代码作为应用程序提供的html/css/ts代码的容器。