正在显示
1 个修改的文件
包含
30 行增加
和
26 行删除
| @@ -3,50 +3,54 @@ import { debounce } from '@/utils' | @@ -3,50 +3,54 @@ import { debounce } from '@/utils' | ||
| 3 | export default { | 3 | export default { |
| 4 | data() { | 4 | data() { |
| 5 | return { | 5 | return { |
| 6 | - $_sidebarElm: null | 6 | + $_sidebarElm: null, |
| 7 | + $_resizeHandler: null | ||
| 7 | } | 8 | } |
| 8 | }, | 9 | }, |
| 9 | mounted() { | 10 | mounted() { |
| 10 | - this.$_initResizeEvent() | ||
| 11 | - this.$_initSidebarResizeEvent() | ||
| 12 | - }, | ||
| 13 | - beforeDestroy() { | ||
| 14 | - this.$_destroyResizeEvent() | ||
| 15 | - this.$_destroySidebarResizeEvent() | 11 | + this.initListener() |
| 16 | }, | 12 | }, |
| 17 | activated() { | 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 | deactivated() { | 25 | deactivated() { |
| 22 | - this.$_destroyResizeEvent() | ||
| 23 | - this.$_destroySidebarResizeEvent() | 26 | + this.destroyListener() |
| 24 | }, | 27 | }, |
| 25 | methods: { | 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 | $_sidebarResizeHandler(e) { | 31 | $_sidebarResizeHandler(e) { |
| 40 | if (e.propertyName === 'width') { | 32 | if (e.propertyName === 'width') { |
| 41 | this.$_resizeHandler() | 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 | this.$_sidebarElm = document.getElementsByClassName('sidebar-container')[0] | 42 | this.$_sidebarElm = document.getElementsByClassName('sidebar-container')[0] |
| 46 | this.$_sidebarElm && this.$_sidebarElm.addEventListener('transitionend', this.$_sidebarResizeHandler) | 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 | this.$_sidebarElm && this.$_sidebarElm.removeEventListener('transitionend', this.$_sidebarResizeHandler) | 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 | } |
-
请 注册 或 登录 后发表评论