欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

vue中实现模态框(通用写法)

程序员文章站 2022-03-17 19:24:47
...
下面我就为大家分享一篇vue实现模态框的通用写法推荐,具有很好的参考价值,希望对大家有所帮助。

在看了element组件的源码后发现,所有模态框其实实现方法都差不多,主要用到了vue在组件化上的双向绑定。代码:

<!--查看槽点对话框-->
<template lang="html">
 <transition name="el-fade-in-linear">
  <p draggable="true" @drag="mouseDrag" @dragend="mouseDragend" :style="dialogStyle" class="g-dialog-wrapper" v-show="myVisible">
   <p class="g-dialog-header">
    <p class="left">
     模态框
    </p>
    <p class="right">
     <i class="g-times-icon fa fa-times" @click="myVisible=false" aria-hidden="true"></i>
    </p>
   </p>
   <p class="g-dialog-container">
   </p>
  </p>
 </transition>
</template>
<script>
 export default {
  props: {
   visible: Boolean
  },
  created() {
  },
  data() {
   return {
    myVisible: this.visible,
  },
  computed: {},
  methods: {
  },
  components: {},
  watch: {
   myVisible: function (val) {
    this.$emit('update:visible', val)
   },
   visible: function (val) {
    this.myVisible = val
   }
  }
 }
</script>
<style lang="css" scoped>
</style>

上面代码主要的部分是watch里面的代码,实现监听数据变化,及时更新。所以在使用的时候就很方便了,在component注册组件之后:

<g-key-dialog :visible.sync="keyDialogVisible"></g-key-dialog>

注:这里必须使用sync,要不是无法双向绑定的

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

解决vue页面刷新或者后退参数丢失的问题

vue页面离开后执行函数的实例

vue轮播图插件vue-concise-slider的使用

以上就是vue中实现模态框(通用写法)的详细内容,更多请关注其它相关文章!