Commit bc65e59fcb391e69a50f1ef4d26957b2f88f4568
1 parent
56339214
修复charts切换侧边栏或者缩放窗口显示bug
Showing
1 changed file
with
30 additions
and
26 deletions
ruoyi-ui/src/views/dashboard/mixins/resize.js
| ... | ... | @@ -3,50 +3,54 @@ import { debounce } from '@/utils' |
| 3 | 3 | export default { |
| 4 | 4 | data() { |
| 5 | 5 | return { |
| 6 | - $_sidebarElm: null | |
| 6 | + $_sidebarElm: null, | |
| 7 | + $_resizeHandler: null | |
| 7 | 8 | } |
| 8 | 9 | }, |
| 9 | 10 | mounted() { |
| 10 | - this.$_initResizeEvent() | |
| 11 | - this.$_initSidebarResizeEvent() | |
| 12 | - }, | |
| 13 | - beforeDestroy() { | |
| 14 | - this.$_destroyResizeEvent() | |
| 15 | - this.$_destroySidebarResizeEvent() | |
| 11 | + this.initListener() | |
| 16 | 12 | }, |
| 17 | 13 | activated() { |
| 18 | - this.$_initResizeEvent() | |
| 19 | - this.$_initSidebarResizeEvent() | |
| 14 | + if (!this.$_resizeHandler) { | |
| 15 | + // avoid duplication init | |
| 16 | + this.initListener() | |
| 17 | + } | |
| 18 | + | |
| 19 | + // when keep-alive chart activated, auto resize | |
| 20 | + this.resize() | |
| 21 | + }, | |
| 22 | + beforeDestroy() { | |
| 23 | + this.destroyListener() | |
| 20 | 24 | }, |
| 21 | 25 | deactivated() { |
| 22 | - this.$_destroyResizeEvent() | |
| 23 | - this.$_destroySidebarResizeEvent() | |
| 26 | + this.destroyListener() | |
| 24 | 27 | }, |
| 25 | 28 | methods: { |
| 26 | - $_resizeHandler() { | |
| 27 | - return debounce(() => { | |
| 28 | - if (this.chart) { | |
| 29 | - this.chart.resize() | |
| 30 | - } | |
| 31 | - }, 100)() | |
| 32 | - }, | |
| 33 | - $_initResizeEvent() { | |
| 34 | - window.addEventListener('resize', this.$_resizeHandler) | |
| 35 | - }, | |
| 36 | - $_destroyResizeEvent() { | |
| 37 | - window.removeEventListener('resize', this.$_resizeHandler) | |
| 38 | - }, | |
| 29 | + // use $_ for mixins properties | |
| 30 | + // https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential | |
| 39 | 31 | $_sidebarResizeHandler(e) { |
| 40 | 32 | if (e.propertyName === 'width') { |
| 41 | 33 | this.$_resizeHandler() |
| 42 | 34 | } |
| 43 | 35 | }, |
| 44 | - $_initSidebarResizeEvent() { | |
| 36 | + initListener() { | |
| 37 | + this.$_resizeHandler = debounce(() => { | |
| 38 | + this.resize() | |
| 39 | + }, 100) | |
| 40 | + window.addEventListener('resize', this.$_resizeHandler) | |
| 41 | + | |
| 45 | 42 | this.$_sidebarElm = document.getElementsByClassName('sidebar-container')[0] |
| 46 | 43 | this.$_sidebarElm && this.$_sidebarElm.addEventListener('transitionend', this.$_sidebarResizeHandler) |
| 47 | 44 | }, |
| 48 | - $_destroySidebarResizeEvent() { | |
| 45 | + destroyListener() { | |
| 46 | + window.removeEventListener('resize', this.$_resizeHandler) | |
| 47 | + this.$_resizeHandler = null | |
| 48 | + | |
| 49 | 49 | this.$_sidebarElm && this.$_sidebarElm.removeEventListener('transitionend', this.$_sidebarResizeHandler) |
| 50 | + }, | |
| 51 | + resize() { | |
| 52 | + const { chart } = this | |
| 53 | + chart && chart.resize() | |
| 50 | 54 | } |
| 51 | 55 | } |
| 52 | 56 | } | ... | ... |