gpt4 book ai didi

Angular 5. 相互导入类时 typescript 中的循环依赖

转载 作者:太空狗 更新时间:2023-10-29 17:58:33 25 4
gpt4 key购买 nike

在使用 TypeScript 的 Angular 5 应用程序中。当尝试实现组件之间的通信时,我遇到了称为Circular Dependency 的问题。有两个组件radioradio-group:

<radio-group [(value)]='selected'>
<radio value='1'></radio>
<radio value='2'></radio>
<radio value='3'></radio>
</radio-group>

当用户选择和取消选择项目时,它们会相互通信。

组件实现示例RadioGroupComponent:

import { Component, forwardRef, Input, Optional, ContentChildren, 

QueryList, EventEmitter, Output, ChangeDetectorRef, ChangeDetectionStrategy, AfterContentInit, OnChanges } from '@angular/core';
import { RadioGroup } from './radio-group.component';

@Component({
selector: 'radio',
styles: [`:host{cursor:pointer;}`],
template: `<div (click)='check()'>
<span *ngIf='!checked'>⚪️</span>
<span *ngIf='checked'>🔘</span>
<span>Click me. Value: {{value}} Checked: {{checked}}</span>
</div>`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class Radio {
@Input() value: any;
checked = false;
private _radioGroup: RadioGroup;
constructor(@Optional() radioGroup: RadioGroup, public cd: ChangeDetectorRef){
this._radioGroup = radioGroup;
}
check(){
this.checked = true;
if(this._radioGroup){
this._radioGroup.selected = this;
}
this.markForCheck();
}
markForCheck(){
this.cd.markForCheck();
}
}

radio 组件:

import { Component, forwardRef, Input, Optional, ContentChildren, QueryList, EventEmitter, Output, ChangeDetectorRef, ChangeDetectionStrategy, AfterContentInit, OnChanges } from '@angular/core';
import { Radio } from './radio.component';

@Component({
selector: 'radio-group',
template: `<ng-content></ng-content>`,
})
export class RadioGroup implements AfterContentInit, OnChanges{
set selected (component:Radio){
this._selected = component;
this.valueChange.emit(component.value);
}
private _selected:Radio = null;
@Input() value:any;
@Output() valueChange = new EventEmitter();
@ContentChildren(forwardRef(() => Radio)) radioComponents: QueryList<Radio>;

ngAfterContentInit() { this.checkParentComponents();}
ngOnChanges(){ this.checkParentComponents();}
checkParentComponents():void{
this.radioComponents
&& this.radioComponents.forEach(item=>{
item.checked = item.value==this.value;
if(item.checked){ this._selected = item;}
item.markForCheck();
});
}
}

在线示例

Working example with all declarations in one file (stackblitz.com)

Broken example with separated files (stackblitz.com)

问题

我如何解决这个循环依赖问题,并将所有组件和实现放入单独的文件中?随着时间分量越来越重,我该如何将它们切成碎片?

最佳答案

您不应该编辑 RadioRadioGroup 属性。子组件和父组件之间的通信应通过 @Input@Output 完成。

因此从 Radio 构造函数中删除 RadioGroup。相反,您可以执行以下操作,

import { 
Component,
Input,
EventEmitter,
Output,
ChangeDetectorRef,
ChangeDetectionStrategy
} from '@angular/core';

@Component({
selector: 'radio',
styles: [`:host{cursor:pointer;}`],
template: `
<div (click)='check()'>
<span *ngIf='!checked'>⚪️</span>
<span *ngIf='checked'>🔘</span>
<span>Click me. Value: {{value}} Checked: {{checked}}</span>
</div>`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class Radio {
@Input() value: any;
@Output() valueChange: EventEmitter<any> = new EventEmitter();
checked = false;
constructor(public cd: ChangeDetectorRef){
}
check(){
this.checked = true;
this.valueChange.emit(this.value);
this.markForCheck();
}
markForCheck(){
this.cd.markForCheck();
}
}

RadioGroup.component

@Component({
selector: 'radio-group',
template: `<ng-content></ng-content>`,
})
export class RadioGroup implements AfterContentInit, OnChanges, OnDestroy {
set selected(component: Radio) {
this._selected = component;
this.valueChange.emit(component.value);
}
private _selected: Radio = null;
@Input() value: any;
@Output() valueChange = new EventEmitter();
@ContentChildren(forwardRef(() => Radio)) radioComponents: QueryList<Radio>;

subscriptionList = [];

ngAfterContentInit() { this.checkParentComponents(); }
ngOnChanges() { this.checkParentComponents(); }
checkParentComponents(): void {
if (this.radioComponents) {
this.subscriptionList = this.radioComponents.map(item => {
item.checked = item.value === this.value;
if (item.checked) { this._selected = item; }
item.markForCheck();
// subscribe to each child "valueChange" event and return these subscriptions.
return item.valueChange.subscription(value => this.selected = value);
});

}
}

ngOnDestroy() {
// don't forget to unsubscribe.
if (this.subscriptionList && this.subscriptionList.length ) {
this.subscriptionList.forEach(sub => sub.unsubscribe());
}
}
}

关于Angular 5. 相互导入类时 typescript 中的循环依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50002311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com