VUE系列——弹窗代码编写与调用弹窗过程详解

简介: VUE系列——弹窗代码编写与调用弹窗过程详解

前言

本文主要介绍弹窗的代码,包括前端、js、css样式,以及点击按钮调用弹窗的过程详解。

步骤如下:


step1 创建一个弹窗

Modal.vue

template

<template>
  <div class="modal-bg" v-show="show" @mousemove="modalMove" @mouseup="cancelMove">
    <div class="modal-container">
      <div class="modal-header" @mousedown="setStartingPoint">
        {{ title }}
      </div>
      <div class="modal-main">
        <slot></slot>
        <p>弹窗里面</p>
      </div>
      <div class="modal-footer">
        <el-button round @click="cancel">取消</el-button>
        <el-button type="primary" round @click="submit">确认</el-button>
      </div>
    </div>
  </div>
</template>

script

1. <script><script>
    export default {
    name: 'Modal',
    props: {
      show: {
        type: Boolean,
        default: false
      },
      title: {
        type: String,
        default: '弹窗'
      },
    },
    data() {
      return {
        x: 0,
        y: 0,
        node: null,
        isCanMove: false
      }
    },
    mounted() {
      this.node = document.querySelector('.modal-container')
    },
    methods: {
      cancel() {
        this.$emit('cancel')
      },
      submit() {
        this.$emit('submit')
      },
      setStartingPoint(e) {
        this.x = e.clientX - this.node.offsetLeft
        this.y = e.clientY - this.node.offsetTop
        this.isCanMove = true
      },
      modalMove(e) {
        if (this.isCanMove) {
          this.node.style.left = e.clientX - this.x + 'px'
          this.node.style.top = e.clientY - this.y + 'px'
        }
      },
      cancelMove() {
        this.isCanMove = false
      }
    }
    }
</script>

style

<style scoped>
  .modal-bg {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,.5);
    z-index: 10;
  }
  .modal-container {
    background: #fff;
    border-radius: 10px;
    overflow: hidden;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }
  .modal-header {
    height: 56px;
    background: #409EFF;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: move;
  }
  .modal-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 57px;
    border-top: 1px solid #ddd;
  }
  .modal-footer button {
    width: 100px;
  }
  .modal-main {
    padding: 15px 40px;
  }
</style>

step2 在router里面引入Modal.vue

1. importimport modal from '../components/Modal';
return[{ 
    path: 'modal', 
    name: '弹窗', 
    component: modal 
}]

step3 调用

template

<Modal :show="show" @cancel="cancel" @submit="submit"></Modal>

script

import Modal from '../../components/Modal';
export default {
  data() {
    return {
      show: false,
    }
  },
  components: {
    Modal
  },
  methods: {
    cancel() {
      // 取消弹窗回调
      this.show = false
    },
    submit() {
      // 确认弹窗回调
      this.show = false
    },
    showWindow(){
      this.show = true
    },
    changeRoute() {
      this.$router.push('/welcome/page2');
    }
  }
};


OK, GAME OVER!

相关文章
|
1天前
|
JavaScript
Vue学习之--------深入理解Vuex之多组件共享数据(2022/9/4)
这篇文章通过一个实际的Vue项目案例,演示了如何在Vuex中实现多组件间共享数据。文章内容包括在Vuex的state中新增用户数组,创建Person.vue组件用于展示和添加用户信息,以及在Count组件中使用Person组件操作的数据。通过测试效果展示了组件间数据共享和状态更新的流程。
Vue学习之--------深入理解Vuex之多组件共享数据(2022/9/4)
|
1天前
|
JavaScript
Vue学习之--------深入理解Vuex之模块化编码(2022/9/4)
这篇文章详细介绍了Vuex的模块化编码和命名空间的使用,旨在让代码更易于维护并提高数据分类的明确性。内容包括模块化和命名空间的概念、如何在store中配置模块、以及如何在组件中使用模块化的数据。文章通过实战项目案例,展示了如何拆分`store/index.js`文件,创建`count.js`和`person.js`模块,并在`Count.vue`和`Person.vue`组件中使用这些模块。最后,文章还提供了测试效果和一些使用注意点。
Vue学习之--------深入理解Vuex之模块化编码(2022/9/4)
|
1天前
|
存储 JavaScript 前端开发
Vue学习之--------路由(Router)的基本使用(1)(2022/9/5)
这篇文章是关于Vue-router路由的基本使用教程,涵盖了安装配置、应用插件、编写路由规则、实现页面跳转和高亮显示,以及一些使用中的注意点和项目实际应用案例。
Vue学习之--------路由(Router)的基本使用(1)(2022/9/5)
|
1天前
|
缓存 JavaScript
Vue学习之--------多级路由的使用(2)(2022/9/5)
这篇文章介绍了在Vue中实现多级路由缓存的方法,包括在路由配置中添加meta属性以启用缓存,使用keep-alive组件包裹需要缓存的视图,以及在Vuex中管理缓存视图列表的逻辑。
Vue学习之--------多级路由的使用(2)(2022/9/5)
|
1天前
|
JavaScript 前端开发 开发者
Vue学习之--------深入理解Vuex、原理详解、实战应用(2022/9/1)
这篇文章详细介绍了Vuex的基本概念、使用场景、安装配置、基本用法、实际应用案例以及注意事项,通过一个数字累加器的实战示例,帮助开发者深入理解Vuex的原理和应用。
|
1天前
|
存储 JavaScript API
vue后台管理权限码处理
【8月更文挑战第19天】vue后台管理权限码处理
8 0
|
1天前
|
JavaScript
Vue学习之--------深入理解Vuex之getters、mapState、mapGetters(2022/9/3)
这篇文章深入探讨了Vuex中的getters概念和用法,以及如何通过mapState、mapGetters、mapActions和mapMutations四个辅助函数简化组件中的Vuex状态访问和操作,通过实际项目案例展示了这些概念的应用和效果。
|
4天前
|
JavaScript
Vue中如何设置在执行删除等危险操作时给用户提示(二次确认后执行对应的操作)
这篇文章介绍了在Vue项目中如何实现执行删除等危险操作时的二次确认机制,使用Element UI的`el-popconfirm`组件来弹出确认框,确保用户在二次确认后才会执行删除操作。
Vue中如何设置在执行删除等危险操作时给用户提示(二次确认后执行对应的操作)
|
4天前
|
JavaScript
如何创建一个Vue项目(手把手教你)
这篇文章是一篇手把手教读者如何创建Vue项目的教程,包括使用管理员身份打开命令行窗口、找到存放项目的位置、通过vue-cli初始化项目、填写项目信息、进入项目目录、启动项目等步骤,并提供了一些常见第三方库的引入方法。
如何创建一个Vue项目(手把手教你)
|
4天前
|
前端开发
StringBoot+Vue实现游客或用户未登录系统前、可以浏览商品等信息、但是不能购买商品或者加入购物车等操作。登录系统显示用户的登录名(源码+讲解)
这篇文章介绍了使用StringBoot+Vue实现用户登录状态判断的方法,包括前端加载用户信息和后端设置session的源码示例。