zhigang.li 2018-03-21 19:20:01 +08:00
parent a00e989ba3
commit 1f7c24bcb7
2 changed files with 21 additions and 16 deletions

View file

@ -1,10 +1,13 @@
<template> <template>
<div class="layout-demo-con"> <div class="layout-demo-con">
<Button @click="change">修改Sider绑定的变量来控制收缩</Button>
<Layout :style="{minHeight: '100vh'}"> <Layout :style="{minHeight: '100vh'}">
<Sider <Sider
v-model="isCollapsed" v-model="isCollapsed"
collapsed-width="0" collapsed-width="0"
hide-trigger hide-trigger
breakpoint="sm"
@on-collapse="changed"
collapsible collapsible
ref="side" ref="side"
width="200"> width="200">
@ -52,6 +55,12 @@ export default {
methods: { methods: {
toggleCollapse () { toggleCollapse () {
this.$refs.side.toggleCollapse(); this.$refs.side.toggleCollapse();
},
change () {
this.isCollapsed = !this.isCollapsed;
},
changed (res) {
console.log(res)
} }
}, },
watch: { watch: {

View file

@ -61,8 +61,7 @@
data () { data () {
return { return {
prefixCls: prefixCls, prefixCls: prefixCls,
mediaMatched: false, mediaMatched: false
isCollapsed: false
}; };
}, },
computed: { computed: {
@ -70,7 +69,7 @@
return [ return [
`${prefixCls}`, `${prefixCls}`,
this.siderWidth ? '' : `${prefixCls}-zero-width`, this.siderWidth ? '' : `${prefixCls}-zero-width`,
this.isCollapsed ? `${prefixCls}-collapsed` : '' this.value ? `${prefixCls}-collapsed` : ''
]; ];
}, },
wrapStyles () { wrapStyles () {
@ -84,7 +83,7 @@
triggerClasses () { triggerClasses () {
return [ return [
`${prefixCls}-trigger`, `${prefixCls}-trigger`,
this.isCollapsed ? `${prefixCls}-trigger-collapsed` : '', this.value ? `${prefixCls}-trigger-collapsed` : '',
]; ];
}, },
childClasses () { childClasses () {
@ -104,10 +103,10 @@
]; ];
}, },
siderWidth () { siderWidth () {
return this.collapsible ? (this.isCollapsed ? (this.mediaMatched ? 0 : parseInt(this.collapsedWidth)) : parseInt(this.width)) : this.width; return this.collapsible ? (this.value ? (this.mediaMatched ? 0 : parseInt(this.collapsedWidth)) : parseInt(this.width)) : this.width;
}, },
showZeroTrigger () { showZeroTrigger () {
return this.collapsible ? (this.mediaMatched && !this.hideTrigger || (parseInt(this.collapsedWidth) === 0) && this.isCollapsed && !this.hideTrigger) : false; return this.collapsible ? (this.mediaMatched && !this.hideTrigger || (parseInt(this.collapsedWidth) === 0) && this.value && !this.hideTrigger) : false;
}, },
showBottomTrigger () { showBottomTrigger () {
return this.collapsible ? !this.mediaMatched && !this.hideTrigger : false; return this.collapsible ? !this.mediaMatched && !this.hideTrigger : false;
@ -115,9 +114,8 @@
}, },
methods: { methods: {
toggleCollapse () { toggleCollapse () {
this.isCollapsed = this.collapsible ? !this.isCollapsed : false; let value = this.collapsible ? !this.value : false;
this.$emit('input', this.isCollapsed); this.$emit('input', value);
this.$emit('on-collapse', this.isCollapsed);
}, },
matchMedia () { matchMedia () {
let matchMedia; let matchMedia;
@ -128,23 +126,21 @@
this.mediaMatched = matchMedia(`(max-width: ${dimensionMap[this.breakpoint]})`).matches; this.mediaMatched = matchMedia(`(max-width: ${dimensionMap[this.breakpoint]})`).matches;
if (this.mediaMatched !== mediaMatched) { if (this.mediaMatched !== mediaMatched) {
this.isCollapsed = this.collapsible ? this.mediaMatched : false;
this.$emit('input', this.mediaMatched); this.$emit('input', this.mediaMatched);
this.$emit('on-collapse', this.mediaMatched);
} }
}, },
onWindowResize () { onWindowResize () {
this.matchMedia(); this.matchMedia();
} }
}, },
watch: {
value (stat) {
this.$emit('on-collapse', stat);
}
},
mounted () { mounted () {
if (this.defaultCollapsed) { if (this.defaultCollapsed) {
this.isCollapsed = true;
this.$emit('input', this.defaultCollapsed); this.$emit('input', this.defaultCollapsed);
} else {
if (this.value !== undefined) {
this.isCollapsed = this.value;
}
} }
if (this.breakpoint !== undefined) { if (this.breakpoint !== undefined) {
on(window, 'resize', this.onWindowResize); on(window, 'resize', this.onWindowResize);