vue+slot+transition+v-model实现淡入淡出弹窗效果

1.首先说一下v-model,印象中只有input标签使用?太天真,它是进行双向数据绑定的,有了它再不用多写代码了.

v-model是   :value和@input的语法糖

使用v-model时:

<input v-model="sth"/>

等效于

使用:value和@input时:

<input :value="sth" @input="sth=$event.target.value"/>

 

2.喜闻乐见的代码环节

1)dialog1.vue

<template>
    <div id="dialog-wrapper">
        <div id="dialog-content">
            弹窗内容
            <button @click="close">关闭弹窗</button>
        </div>
    </div>
</template>
<script>
export default {
    name: 'DialogOne',
    props: {
        value: {
            type: Boolean,
            default: false
        }
    },
    methods: {
        close() {
            this.$emit('input',false)
        }
    }
}
</script>
<style lang="stylus" scoped>
#dialog-wrapper
    position absolute
    top 0
    left 0
    right 0
    bottom 0
    background: rgba(0,0,0,0.5)
    display flex
    justify-content center
    align-items center
    #dialog-content
        color red

</style>

 

2)dialog目录下的index.vue(主要是使用了transition动画和slot插槽)

<template>
    <transition>
        <slot></slot>
    </transition>
</template>
<script>
export default {
    name: 'VDialog'
}
</script>
<style lang="stylus" scoped>
//淡入淡出的效果
.v-enter, .v-leave-to
    opacity: 0
.v-enter-active, .v-leave-active
    transition: opacity .5s
</style>

3)在app.vue中使用

<template>
  <div id="app">
    <button @click="showDialog()">出现弹窗</button>
    <v-dialog>
      <dialog-one v-if="ifShow" v-model="ifShow"></dialog-one>
    </v-dialog>
    <router-view/>
  </div>
</template>
<script>
import VDialog from '@/components/common/dialog'
import DialogOne from '@/components/common/dialog/dialog1'
export default {
  components: {
    VDialog,
    DialogOne
  },
  data() {
    return {
      ifShow: false
    }
  },
  methods: {
    showDialog() {
      this.ifShow = true
    }
  }
}
</script>
<style lang="stylus">
html 
  font-size 20px
</style>

重要的是js逻辑部分,样式你们自定义就好

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值