Angular listens to the value changes of sibling components (brother components pass values)

Operational background: the sidebar shrinks, and the container for echarts will change. So after shrinking, echarts redraws or resizes.

Personal opinion: This method is not only to monitor the value of sibling components, but also can be applied to many scenarios and explore by yourself.
1. Create a "middleware" service to handle

@Injectable({
//因为这里我的需求是全局的,所以
providedIn:'root'
})
export class ChangeSideService{
	//创建Subject事件
	private subject = new Subject();
	//创建消费者监听
	listen = this.subject.asObserbale();
	constructor(){}
	sideFlag :boolea;
	setSideFlag(val:boolean){
	this.sideFlag = val;
	this.subject.next(val)
	}
	
}

2. Brother components send value changes

//1.先引入服务
constructor(private changeSide:ChangeSideService){
}
//2.在改变数值的地方发送
this.changeSide.setSideFlag(val);

3. Response to numerical changes

//1.注入服务
constructor(private changeSide:ChangeSideService){
}
//2.作出相应操作
this.changeSide.listen.subscribe((val:boolean)=>{
//TODO
})

Supplement: Later found that this method is a bit stupid.
Angular uses echarts to complete this situation. You can use the command [autoResize]='true' that comes with ngx-echarts

Guess you like

Origin blog.csdn.net/DragonOfMoon/article/details/125302228