Skip to content

Commit

Permalink
feat: 默认 layout 使用处理过的 element 滚动条组件替代 better-scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
FairyEver committed Nov 24, 2020
1 parent f22d220 commit a93e966
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 61 deletions.
34 changes: 34 additions & 0 deletions src/components/d2-scrollbar/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<style lang="scss">
.d2-scrollbar {
height: 100%;
.el-scrollbar__wrap {
overflow: auto;
}
}
</style>

<template>
<el-scrollbar
class="d2-scrollbar"
ref="scrollbar"
v-bind="$attrs"
v-on="$listeners">
<slot/>
</el-scrollbar>
</template>

<script>
import { addResizeListener, removeResizeListener } from 'element-ui/src/utils/resize-event'
export default {
name: 'd2-scrollbar',
inheritAttrs: false,
mounted () {
const scrollbar = this.$refs.scrollbar
addResizeListener(scrollbar.$el, scrollbar.update)
this.$once('hook:beforeDestroy', () => {
removeResizeListener(scrollbar.$el, scrollbar.update)
})
}
}
</script>
1 change: 1 addition & 0 deletions src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ Vue.component('d2-mde', () => import('./d2-mde'))
Vue.component('d2-module-index-banner', () => import('./d2-module-index-banner'))
Vue.component('d2-module-index-menu', () => import('./d2-module-index-menu'))
Vue.component('d2-quill', () => import('./d2-quill'))
Vue.component('d2-scrollbar', () => import('./d2-scrollbar'))
Vue.component('d2-ueditor', () => import('./d2-ueditor'))
80 changes: 19 additions & 61 deletions src/layout/header-aside/components/menu-side/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { mapState } from 'vuex'
import menuMixin from '../mixin/menu'
import { createMenu } from '../libs/util.menu'
import BScroll from 'better-scroll'

export default {
name: 'd2-layout-header-aside-menu-side',
Expand All @@ -10,73 +9,32 @@ export default {
],
render (h) {
return <div class="d2-layout-header-aside-menu-side">
<el-menu
collapse={ this.asideCollapse }
collapseTransition={ this.asideTransition }
uniqueOpened={ true }
defaultActive={ this.$route.fullPath }
ref="menu"
onSelect={ this.handleMenuSelect }>
{ this.aside.map(menu => createMenu.call(this, h, menu)) }
</el-menu>
{
this.aside.length === 0 && !this.asideCollapse
? <div class="d2-layout-header-aside-menu-empty" flex="dir:top main:center cross:center">
<d2-icon name="inbox"/>
<span>没有侧栏菜单</span>
</div>
: null
}
<d2-scrollbar>
<el-menu
collapse={ this.asideCollapse }
collapseTransition={ this.asideTransition }
uniqueOpened={ true }
defaultActive={ this.$route.fullPath }
ref="menu"
onSelect={ this.handleMenuSelect }>
{ this.aside.map(menu => createMenu.call(this, h, menu)) }
</el-menu>
{
this.aside.length === 0 && !this.asideCollapse
? <div class="d2-layout-header-aside-menu-empty" flex="dir:top main:center cross:center">
<d2-icon name="inbox"/>
<span>没有侧栏菜单</span>
</div>
: null
}
</d2-scrollbar>
</div>
},
data () {
return {
asideHeight: 300,
BS: null
}
},
computed: {
...mapState('d2admin/menu', [
'aside',
'asideCollapse',
'asideTransition'
])
},
watch: {
// 折叠和展开菜单的时候销毁 better scroll
asideCollapse (val) {
this.scrollDestroy()
setTimeout(() => {
this.scrollInit()
}, 500)
}
},
mounted () {
this.scrollInit()
},
beforeDestroy () {
this.scrollDestroy()
},
methods: {
scrollInit () {
this.BS = new BScroll(this.$el, {
mouseWheel: true,
click: true
// 如果你愿意可以打开显示滚动条
// scrollbar: {
// fade: true,
// interactive: false
// }
})
},
scrollDestroy () {
// https://github.com/d2-projects/d2-admin/issues/75
try {
this.BS.destroy()
} catch (e) {
delete this.BS
this.BS = null
}
}
}
}

0 comments on commit a93e966

Please sign in to comment.