fix[Chart]: fixed chart bug in keep-alive (#2119)
This commit is contained in:
		| @@ -5,11 +5,12 @@ | |||||||
| <script> | <script> | ||||||
| import echarts from 'echarts' | import echarts from 'echarts' | ||||||
| require('echarts/theme/macarons') // echarts theme | require('echarts/theme/macarons') // echarts theme | ||||||
| import { debounce } from '@/utils' | import resize from './mixins/resize' | ||||||
|  |  | ||||||
| const animationDuration = 6000 | const animationDuration = 6000 | ||||||
|  |  | ||||||
| export default { | export default { | ||||||
|  |   mixins: [resize], | ||||||
|   props: { |   props: { | ||||||
|     className: { |     className: { | ||||||
|       type: String, |       type: String, | ||||||
| @@ -30,19 +31,14 @@ export default { | |||||||
|     } |     } | ||||||
|   }, |   }, | ||||||
|   mounted() { |   mounted() { | ||||||
|     this.initChart() |     this.$nextTick(() => { | ||||||
|     this.__resizeHandler = debounce(() => { |       this.initChart() | ||||||
|       if (this.chart) { |     }) | ||||||
|         this.chart.resize() |  | ||||||
|       } |  | ||||||
|     }, 100) |  | ||||||
|     window.addEventListener('resize', this.__resizeHandler) |  | ||||||
|   }, |   }, | ||||||
|   beforeDestroy() { |   beforeDestroy() { | ||||||
|     if (!this.chart) { |     if (!this.chart) { | ||||||
|       return |       return | ||||||
|     } |     } | ||||||
|     window.removeEventListener('resize', this.__resizeHandler) |  | ||||||
|     this.chart.dispose() |     this.chart.dispose() | ||||||
|     this.chart = null |     this.chart = null | ||||||
|   }, |   }, | ||||||
|   | |||||||
| @@ -5,9 +5,10 @@ | |||||||
| <script> | <script> | ||||||
| import echarts from 'echarts' | import echarts from 'echarts' | ||||||
| require('echarts/theme/macarons') // echarts theme | require('echarts/theme/macarons') // echarts theme | ||||||
| import { debounce } from '@/utils' | import resize from './mixins/resize' | ||||||
|  |  | ||||||
| export default { | export default { | ||||||
|  |   mixins: [resize], | ||||||
|   props: { |   props: { | ||||||
|     className: { |     className: { | ||||||
|       type: String, |       type: String, | ||||||
| @@ -32,8 +33,7 @@ export default { | |||||||
|   }, |   }, | ||||||
|   data() { |   data() { | ||||||
|     return { |     return { | ||||||
|       chart: null, |       chart: null | ||||||
|       sidebarElm: null |  | ||||||
|     } |     } | ||||||
|   }, |   }, | ||||||
|   watch: { |   watch: { | ||||||
| @@ -45,38 +45,21 @@ export default { | |||||||
|     } |     } | ||||||
|   }, |   }, | ||||||
|   mounted() { |   mounted() { | ||||||
|     this.initChart() |     this.$nextTick(() => { | ||||||
|     if (this.autoResize) { |       this.initChart() | ||||||
|       this.__resizeHandler = debounce(() => { |     }) | ||||||
|         if (this.chart) { |  | ||||||
|           this.chart.resize() |  | ||||||
|         } |  | ||||||
|       }, 100) |  | ||||||
|       window.addEventListener('resize', this.__resizeHandler) |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     // 监听侧边栏的变化 |  | ||||||
|     this.sidebarElm = document.getElementsByClassName('sidebar-container')[0] |  | ||||||
|     this.sidebarElm && this.sidebarElm.addEventListener('transitionend', this.sidebarResizeHandler) |  | ||||||
|   }, |   }, | ||||||
|   beforeDestroy() { |   beforeDestroy() { | ||||||
|     if (!this.chart) { |     if (!this.chart) { | ||||||
|       return |       return | ||||||
|     } |     } | ||||||
|     if (this.autoResize) { |  | ||||||
|       window.removeEventListener('resize', this.__resizeHandler) |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     this.sidebarElm && this.sidebarElm.removeEventListener('transitionend', this.sidebarResizeHandler) |  | ||||||
|  |  | ||||||
|     this.chart.dispose() |     this.chart.dispose() | ||||||
|     this.chart = null |     this.chart = null | ||||||
|   }, |   }, | ||||||
|   methods: { |   methods: { | ||||||
|     sidebarResizeHandler(e) { |     initChart() { | ||||||
|       if (e.propertyName === 'width') { |       this.chart = echarts.init(this.$el, 'macarons') | ||||||
|         this.__resizeHandler() |       this.setOptions(this.chartData) | ||||||
|       } |  | ||||||
|     }, |     }, | ||||||
|     setOptions({ expectedData, actualData } = {}) { |     setOptions({ expectedData, actualData } = {}) { | ||||||
|       this.chart.setOption({ |       this.chart.setOption({ | ||||||
| @@ -146,10 +129,6 @@ export default { | |||||||
|           animationEasing: 'quadraticOut' |           animationEasing: 'quadraticOut' | ||||||
|         }] |         }] | ||||||
|       }) |       }) | ||||||
|     }, |  | ||||||
|     initChart() { |  | ||||||
|       this.chart = echarts.init(this.$el, 'macarons') |  | ||||||
|       this.setOptions(this.chartData) |  | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -5,9 +5,10 @@ | |||||||
| <script> | <script> | ||||||
| import echarts from 'echarts' | import echarts from 'echarts' | ||||||
| require('echarts/theme/macarons') // echarts theme | require('echarts/theme/macarons') // echarts theme | ||||||
| import { debounce } from '@/utils' | import resize from './mixins/resize' | ||||||
|  |  | ||||||
| export default { | export default { | ||||||
|  |   mixins: [resize], | ||||||
|   props: { |   props: { | ||||||
|     className: { |     className: { | ||||||
|       type: String, |       type: String, | ||||||
| @@ -28,19 +29,14 @@ export default { | |||||||
|     } |     } | ||||||
|   }, |   }, | ||||||
|   mounted() { |   mounted() { | ||||||
|     this.initChart() |     this.$nextTick(() => { | ||||||
|     this.__resizeHandler = debounce(() => { |       this.initChart() | ||||||
|       if (this.chart) { |     }) | ||||||
|         this.chart.resize() |  | ||||||
|       } |  | ||||||
|     }, 100) |  | ||||||
|     window.addEventListener('resize', this.__resizeHandler) |  | ||||||
|   }, |   }, | ||||||
|   beforeDestroy() { |   beforeDestroy() { | ||||||
|     if (!this.chart) { |     if (!this.chart) { | ||||||
|       return |       return | ||||||
|     } |     } | ||||||
|     window.removeEventListener('resize', this.__resizeHandler) |  | ||||||
|     this.chart.dispose() |     this.chart.dispose() | ||||||
|     this.chart = null |     this.chart = null | ||||||
|   }, |   }, | ||||||
|   | |||||||
| @@ -5,11 +5,12 @@ | |||||||
| <script> | <script> | ||||||
| import echarts from 'echarts' | import echarts from 'echarts' | ||||||
| require('echarts/theme/macarons') // echarts theme | require('echarts/theme/macarons') // echarts theme | ||||||
| import { debounce } from '@/utils' | import resize from './mixins/resize' | ||||||
|  |  | ||||||
| const animationDuration = 3000 | const animationDuration = 3000 | ||||||
|  |  | ||||||
| export default { | export default { | ||||||
|  |   mixins: [resize], | ||||||
|   props: { |   props: { | ||||||
|     className: { |     className: { | ||||||
|       type: String, |       type: String, | ||||||
| @@ -30,19 +31,14 @@ export default { | |||||||
|     } |     } | ||||||
|   }, |   }, | ||||||
|   mounted() { |   mounted() { | ||||||
|     this.initChart() |     this.$nextTick(() => { | ||||||
|     this.__resizeHandler = debounce(() => { |       this.initChart() | ||||||
|       if (this.chart) { |     }) | ||||||
|         this.chart.resize() |  | ||||||
|       } |  | ||||||
|     }, 100) |  | ||||||
|     window.addEventListener('resize', this.__resizeHandler) |  | ||||||
|   }, |   }, | ||||||
|   beforeDestroy() { |   beforeDestroy() { | ||||||
|     if (!this.chart) { |     if (!this.chart) { | ||||||
|       return |       return | ||||||
|     } |     } | ||||||
|     window.removeEventListener('resize', this.__resizeHandler) |  | ||||||
|     this.chart.dispose() |     this.chart.dispose() | ||||||
|     this.chart = null |     this.chart = null | ||||||
|   }, |   }, | ||||||
|   | |||||||
							
								
								
									
										56
									
								
								src/views/dashboard/admin/components/mixins/resize.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								src/views/dashboard/admin/components/mixins/resize.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,56 @@ | |||||||
|  | import { debounce } from '@/utils' | ||||||
|  |  | ||||||
|  | export default { | ||||||
|  |   data() { | ||||||
|  |     return { | ||||||
|  |       $_sidebarElm: null | ||||||
|  |     } | ||||||
|  |   }, | ||||||
|  |   mounted() { | ||||||
|  |     this.$_initResizeEvent() | ||||||
|  |     this.$_initSidebarResizeEvent() | ||||||
|  |   }, | ||||||
|  |   beforeDestroy() { | ||||||
|  |     this.$_destroyResizeEvent() | ||||||
|  |     this.$_destroySidebarResizeEvent() | ||||||
|  |   }, | ||||||
|  |   // to fixed bug when cached by keep-alive | ||||||
|  |   // https://github.com/PanJiaChen/vue-element-admin/issues/2116 | ||||||
|  |   activated() { | ||||||
|  |     this.$_initResizeEvent() | ||||||
|  |     this.$_initSidebarResizeEvent() | ||||||
|  |   }, | ||||||
|  |   deactivated() { | ||||||
|  |     this.$_destroyResizeEvent() | ||||||
|  |     this.$_destroySidebarResizeEvent() | ||||||
|  |   }, | ||||||
|  |   methods: { | ||||||
|  |     // use $_ for mixins properties | ||||||
|  |     // https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential | ||||||
|  |     $_resizeHandler() { | ||||||
|  |       return debounce(() => { | ||||||
|  |         if (this.chart) { | ||||||
|  |           this.chart.resize() | ||||||
|  |         } | ||||||
|  |       }, 100)() | ||||||
|  |     }, | ||||||
|  |     $_initResizeEvent() { | ||||||
|  |       window.addEventListener('resize', this.$_resizeHandler) | ||||||
|  |     }, | ||||||
|  |     $_destroyResizeEvent() { | ||||||
|  |       window.removeEventListener('resize', this.$_resizeHandler) | ||||||
|  |     }, | ||||||
|  |     $_sidebarResizeHandler(e) { | ||||||
|  |       if (e.propertyName === 'width') { | ||||||
|  |         this.$_resizeHandler() | ||||||
|  |       } | ||||||
|  |     }, | ||||||
|  |     $_initSidebarResizeEvent() { | ||||||
|  |       this.$_sidebarElm = document.getElementsByClassName('sidebar-container')[0] | ||||||
|  |       this.$_sidebarElm && this.$_sidebarElm.addEventListener('transitionend', this.$_sidebarResizeHandler) | ||||||
|  |     }, | ||||||
|  |     $_destroySidebarResizeEvent() { | ||||||
|  |       this.$_sidebarElm && this.$_sidebarElm.removeEventListener('transitionend', this.$_sidebarResizeHandler) | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user