vue2二次封装element ui的抽屉组件el-drawer

5 篇文章 0 订阅
1 篇文章 0 订阅

1、父组件

<template>
  <div class="test">
    <button @click="openDrawer">打开抽屉</button>
    <drawer-component :visible.sync="drawerVisible" ref="drawerComponent" title="我是抽屉标题" @closed="handleDrawerClosed"></drawer-component>
  </div>
</template>
<script>
import DrawerComponent from './components/Drawer'
export default {
  data() {
    return{
      drawerVisible: false,
    }
  },
  components:{
    DrawerComponent,
  },
  methods:{
    openDrawer() {
      this.drawerVisible = true;
      console.log(this.drawerVisible);
    },
    handleDrawerClosed() {
      this.drawerVisible = false;
      console.log('抽屉已关闭');
    },
  }
}
</script>
<style scoped>
.test{width: 100vw;height: 100vh;background: #ccc}
</style>

2、子组件

<template>
  <div>
    <el-drawer
        :visible.sync="localVisible"
        :size="drawersize"
        :destroy-on-close="true"
        :direction="direction"
        @close="handleClose">
      <div class="chatModal">
        asdasdasdasda
      </div>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "DrawerComponent",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      drawersize: "540px",
      direction: "rtl",
      localVisible: this.visible
    };
  },
  watch: {
    visible(newVal) {
      this.localVisible = newVal;
    },
    localVisible(newVal) {
      this.$emit('update:visible', newVal);
    }
  },
  methods: {
    handleClose() {
      this.localVisible = false;
      this.$emit('closed');
    }
  },
  mounted() {
    console.log(this.visible);
  },
}
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值