我遇到的问题是:使用sortable.js插件进行拖拽列表,列表里使用了echarts插件,多次拖拽同一个echarts会导致echarts内容丢失
使用的技术栈:angular12、sortableJs、echarts
这是我echarts的ts
import { AfterViewInit, Component, ElementRef, Input, OnInit, ViewChild } from '@angular/core';
import { DemandService } from '../demand.service';
import * as echarts from 'echarts';
@Component({
// eslint-disable-next-line @angular-eslint/component-selector
selector: 'ori-echarts',
templateUrl: './echarts.component.html',
styleUrls: ['./echarts.component.less']
})
export class EchartsComponent implements OnInit, AfterViewInit {
constructor(private ds: DemandService) { }
@Input() options;
@ViewChild('chartContainer') chartContainer!: ElementRef;
chartInstance
ngOnInit(): void {}
ngAfterViewInit(): void {
this.initEcahrts();
}
echartsInstanceIds = [];
initEcahrts() {
this.updateEcharts('init');
}
refreshEcharts() {
this.updateEcharts('refresh');
}
updateEcharts(optionType: 'init'|'refresh') {
const chartElement = document.createElement('div');
chartElement.style.width = '100%';
chartElement.style.height = '100%';
chartElement.id = 'ec_' + new Date().getTime();
if(optionType === 'refresh') {
if (this.chartContainer.nativeElement.querySelector('div')) {
this.chartContainer.nativeElement.querySelector('div').remove();
}
}
this.chartContainer.nativeElement.appendChild(chartElement);
this.chartInstance = echarts.init(chartElement);
this.chartInstance.setOption(this.options);
this.chartInstance.resize();
}
}
这是我echarts的html
<div #chartContainer style="width: 100%; height: 100%;"></div>
我在拖拽文件里写了如下代码片段
addProxyBindingCopyDrag() {
this.ds.proxySortableListDom = document.getElementById('proxy-sort-list');
const that = this;
const sortable = Sortable.create(this.ds.proxySortableListDom, {
group: {
name: 'proxyDrag',
pull: 'clone',
put: false
},
removeCloneOnHide: false,
filter: '.has-dragged-item',
animation: 150,
sort: false,
onStart: (evt) => {
const index = evt.clone.getAttribute('index');
this.dppChartComponent.refreshEcharts();
},
onEnd: (evt) => {
const index = evt.clone.getAttribute('index');
if (!that.ds.proxyList[index].dragged) {
that.ds.proxyList[index].dragged = true;
}
this.dppChartComponent.refreshEcharts();
}
});
}
结果第一次拖拽是好的
但到了第二次拖拽同一个图的时候
我本来以为是echarts的实例id相同的问题,但是现在的实现效果,每一次拖拽过来的echarts图实现了_echarts_instance_不同了,echarts还是会丢失
现在我不太清楚问题出在哪里了,请各位指点迷津!