Skip to content

Commit 13c1ecf

Browse files
authored
fix[Chart]: fixed chart bug in keep-alive (PanJiaChen#2119)
1 parent 8ce250a commit 13c1ecf

File tree

5 files changed

+68
-54
lines changed

5 files changed

+68
-54
lines changed

src/views/dashboard/admin/components/BarChart.vue

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
<script>
66
import echarts from 'echarts'
77
require('echarts/theme/macarons') // echarts theme
8-
import { debounce } from '@/utils'
8+
import resize from './mixins/resize'
99
1010
const animationDuration = 6000
1111
1212
export default {
13+
mixins: [resize],
1314
props: {
1415
className: {
1516
type: String,
@@ -33,18 +34,11 @@ export default {
3334
this.$nextTick(() => {
3435
this.initChart()
3536
})
36-
this.__resizeHandler = debounce(() => {
37-
if (this.chart) {
38-
this.chart.resize()
39-
}
40-
}, 100)
41-
window.addEventListener('resize', this.__resizeHandler)
4237
},
4338
beforeDestroy() {
4439
if (!this.chart) {
4540
return
4641
}
47-
window.removeEventListener('resize', this.__resizeHandler)
4842
this.chart.dispose()
4943
this.chart = null
5044
},

src/views/dashboard/admin/components/LineChart.vue

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
<script>
66
import echarts from 'echarts'
77
require('echarts/theme/macarons') // echarts theme
8-
import { debounce } from '@/utils'
8+
import resize from './mixins/resize'
99
1010
export default {
11+
mixins: [resize],
1112
props: {
1213
className: {
1314
type: String,
@@ -32,8 +33,7 @@ export default {
3233
},
3334
data() {
3435
return {
35-
chart: null,
36-
sidebarElm: null
36+
chart: null
3737
}
3838
},
3939
watch: {
@@ -48,38 +48,18 @@ export default {
4848
this.$nextTick(() => {
4949
this.initChart()
5050
})
51-
52-
if (this.autoResize) {
53-
this.__resizeHandler = debounce(() => {
54-
if (this.chart) {
55-
this.chart.resize()
56-
}
57-
}, 100)
58-
window.addEventListener('resize', this.__resizeHandler)
59-
}
60-
61-
// 监听侧边栏的变化
62-
this.sidebarElm = document.getElementsByClassName('sidebar-container')[0]
63-
this.sidebarElm && this.sidebarElm.addEventListener('transitionend', this.sidebarResizeHandler)
6451
},
6552
beforeDestroy() {
6653
if (!this.chart) {
6754
return
6855
}
69-
if (this.autoResize) {
70-
window.removeEventListener('resize', this.__resizeHandler)
71-
}
72-
73-
this.sidebarElm && this.sidebarElm.removeEventListener('transitionend', this.sidebarResizeHandler)
74-
7556
this.chart.dispose()
7657
this.chart = null
7758
},
7859
methods: {
79-
sidebarResizeHandler(e) {
80-
if (e.propertyName === 'width') {
81-
this.__resizeHandler()
82-
}
60+
initChart() {
61+
this.chart = echarts.init(this.$el, 'macarons')
62+
this.setOptions(this.chartData)
8363
},
8464
setOptions({ expectedData, actualData } = {}) {
8565
this.chart.setOption({
@@ -149,10 +129,6 @@ export default {
149129
animationEasing: 'quadraticOut'
150130
}]
151131
})
152-
},
153-
initChart() {
154-
this.chart = echarts.init(this.$el, 'macarons')
155-
this.setOptions(this.chartData)
156132
}
157133
}
158134
}

src/views/dashboard/admin/components/PieChart.vue

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
<script>
66
import echarts from 'echarts'
77
require('echarts/theme/macarons') // echarts theme
8-
import { debounce } from '@/utils'
8+
import resize from './mixins/resize'
99
1010
export default {
11+
mixins: [resize],
1112
props: {
1213
className: {
1314
type: String,
@@ -31,18 +32,11 @@ export default {
3132
this.$nextTick(() => {
3233
this.initChart()
3334
})
34-
this.__resizeHandler = debounce(() => {
35-
if (this.chart) {
36-
this.chart.resize()
37-
}
38-
}, 100)
39-
window.addEventListener('resize', this.__resizeHandler)
4035
},
4136
beforeDestroy() {
4237
if (!this.chart) {
4338
return
4439
}
45-
window.removeEventListener('resize', this.__resizeHandler)
4640
this.chart.dispose()
4741
this.chart = null
4842
},

src/views/dashboard/admin/components/RaddarChart.vue

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
<script>
66
import echarts from 'echarts'
77
require('echarts/theme/macarons') // echarts theme
8-
import { debounce } from '@/utils'
8+
import resize from './mixins/resize'
99
1010
const animationDuration = 3000
1111
1212
export default {
13+
mixins: [resize],
1314
props: {
1415
className: {
1516
type: String,
@@ -33,18 +34,11 @@ export default {
3334
this.$nextTick(() => {
3435
this.initChart()
3536
})
36-
this.__resizeHandler = debounce(() => {
37-
if (this.chart) {
38-
this.chart.resize()
39-
}
40-
}, 100)
41-
window.addEventListener('resize', this.__resizeHandler)
4237
},
4338
beforeDestroy() {
4439
if (!this.chart) {
4540
return
4641
}
47-
window.removeEventListener('resize', this.__resizeHandler)
4842
this.chart.dispose()
4943
this.chart = null
5044
},
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { debounce } from '@/utils'
2+
3+
export default {
4+
data() {
5+
return {
6+
$_sidebarElm: null
7+
}
8+
},
9+
mounted() {
10+
this.$_initResizeEvent()
11+
this.$_initSidebarResizeEvent()
12+
},
13+
beforeDestroy() {
14+
this.$_destroyResizeEvent()
15+
this.$_destroySidebarResizeEvent()
16+
},
17+
// to fixed bug when cached by keep-alive
18+
// https://github.com/PanJiaChen/vue-element-admin/issues/2116
19+
activated() {
20+
this.$_initResizeEvent()
21+
this.$_initSidebarResizeEvent()
22+
},
23+
deactivated() {
24+
this.$_destroyResizeEvent()
25+
this.$_destroySidebarResizeEvent()
26+
},
27+
methods: {
28+
// use $_ for mixins properties
29+
// https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential
30+
$_resizeHandler() {
31+
return debounce(() => {
32+
if (this.chart) {
33+
this.chart.resize()
34+
}
35+
}, 100)()
36+
},
37+
$_initResizeEvent() {
38+
window.addEventListener('resize', this.$_resizeHandler)
39+
},
40+
$_destroyResizeEvent() {
41+
window.removeEventListener('resize', this.$_resizeHandler)
42+
},
43+
$_sidebarResizeHandler(e) {
44+
if (e.propertyName === 'width') {
45+
this.$_resizeHandler()
46+
}
47+
},
48+
$_initSidebarResizeEvent() {
49+
this.$_sidebarElm = document.getElementsByClassName('sidebar-container')[0]
50+
this.$_sidebarElm && this.$_sidebarElm.addEventListener('transitionend', this.$_sidebarResizeHandler)
51+
},
52+
$_destroySidebarResizeEvent() {
53+
this.$_sidebarElm && this.$_sidebarElm.removeEventListener('transitionend', this.$_sidebarResizeHandler)
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)