vue组件系列1、弹窗

vue组件系列1、弹窗

建立vue文件 pre_message.vue,直接源码,注释很详细

 <template>
  <div class="my_dialog">
    <!--外层的遮罩 点击事件用来关闭弹窗,isShow控制弹窗显示 隐藏的props-->
    <div
      class="dialog-cover"
      v-if="pre_show"
      @click="closeMyself"
    ></div>
    <!-- transition 这里可以加一些简单的动画效果 -->
    <transition name="drop">
      <!--style 通过props 控制内容的样式  -->
      <div
        class="dialog-content"
        v-if="pre_show"
      >
        <div :class="pre_showtype">
          <!--弹窗头部 title-->
          <slot name="header">{
   {pre_title}}</slot>
        </div>
        <div class="dialog_main">
          <!--弹窗的内容-->
          <slot name="main">{
   {pre_note}}</slot>
        </div>
        <!--弹窗关闭按钮-->
        <div
          class="foot_close"
          @click="closeMyself"
        >
          <div class="close_img back"></div>
        </div>
      </div>
    </transition>
  </div>
</template> 

<script>
/*
名称:弹窗组件
日期:2019-03-14
作者:hj
步骤:
先搭建组件的html和css样式,遮罩层和内容层。
调用:


*/
export default {
  /*组件名称不能和系统关键字重复*/ 
  name: "pre_message",
  data() {
    return {
      pre_show:false,
      pre_showtype:'dialog_head dialog_head_none',
      pre_title:'提示',
      pre_note:'说明信息没有获取到',
    }
  },
  props: {        
    // 展示类型
    showtype:{
      type:String,
      default:''
    },
    // 展示内容
    note:{
      type:String,
      default:''
    },
    message_data: {
      type: Object,
      default: function () {
        return { showtype: 'dialog_head dialog_head_none',note:'对象模式' }
      }
    }
  },
  watch:{    
    note(val){
      this.my_note=val;
      console.log('展示'+this.note+' my_note='+this.my_note);
      this.refresh_data();
    },
    message_data(obj){
      this.check_obj();
    }
  },
  methods: {
    closeMyself() {
      console.log('关闭');
      // this.$emit("on-close");
      this.pre_show=false;
    },
    refresh_data:function(){
      console.log('子组件数据刷新...');
      this.pre_show=true;
      this.pre_title='提示';
      this.pre_note=this.note;
    },
    check_obj:function(){
      // console.log('传入');
      // console.log(this.message_data);
      switch(this.message_data.showtype){
        case 'ok':
          this.pre_showtype='dialog_head dialog_head_ok';
          this.pre_title='通过';
          break;
        case 'no':
          this.pre_showtype='dialog_head dialog_head_no';
          this.pre_title='拒绝';
          break;
        case 'err':
          this.pre_showtype='dialog_head dialog_head_err';
          this.pre_title='错误';
          break;
        case 'warning':
          this.pre_showtype='dialog_head dialog_head_warning';
          this.pre_title='警告';
          break;
        default:
          this.pre_showtype='dialog_head dialog_head_none';
          break;
      }
      this.pre_note=this.message_data.note;
      this.pre_show=true;
    }
  }
};
</script>


<style lang="scss" scoped>
/*
scss 依赖安装
npm  install -s sass-loader
npm install -s node-sass
*/
/** 弹窗动画*/
.drop-enter-active {
  // 动画进入过程:0.5s
  transition: all 0.5s ease;
}
.drop-leave-active {
  // 动画离开过程:0.5s
  transition: all 0.3s ease;
}
.drop-enter {
  //动画之前的位置
  transform: translateY(-500px);
}
.drop-leave-active {
  //动画之后的位置
  transform: translateY(-500px);
}
// 最外层 设置position定位
.my_dialog {
  position: relative;
  color: #2e2c2d;
  font-size: 16px;
}
// 遮罩 设置背景层,z-index值要足够大确保能覆盖,高度 宽度设置满 做到全屏遮罩
.dialog-cover {
  background: rgba(0, 0, 0, 0.8);
  position: fixed;
  z-index: 200;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
// 内容层 z-index要比遮罩大,否则会被遮盖,
.dialog-content {
  position: fixed;
  width: 40%;
  top: 35%;
  left: 30%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 300;

  .dialog_head {
    // 头部title的背景 居中圆角等属性。
    // width: 86.5%;
    width: 100%;
    height: 43px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 24px;
    padding: 5px 50px 10px 50px;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
  }
  .dialog_head_ok {
    background-color: #45f79f;
  }
  .dialog_head_no {
    background-color: #72837a;
  }
  .dialog_head_warning {
    background-color: #f4fb8e;
  }
  .dialog_head_err {
    background-color: #f31b2a;
  }
  .dialog_head_none {
    background-color: #ffffff;
  }
  .dialog_main {
    // 主体内容样式设置
    background: #ffffff;
    display: flex;
    justify-content: center;
    align-content: center;
    // width: 86.5%;
    width: 100%;
    padding: 22px 50px 47px 50px;
    border-bottom-left-radius: 10px;
    border-bottom-right-radius: 10px;
  }
  .foot_close {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #fcca03;
    display: flex;
    justify-content: center;
    align-content: center;
    position: absolute;
    right: -60px;
    top: -6px;
    .close_img {
      background-image: url("../../assets/image/shan1.png");
      width: 20px;
      height: 20px;
    }
  }
}
</style>

父组件使用

<template>
  <div class="mydiv">    
    <button @click="tt">测试</button>
    <button @click="tt_ok">通过</button>
    <button @click="tt_no">拒绝</button>
    <button @click="tt_err">错误</button>
    <button @click="tt_warning">警告</button>
    <!-- 放置自定义组件 -->    
    <per-message
      ref="child1"
      v-bind:note="setnote"
      :message_data="msg_obj"
      @on-close="closeDialog"
    />
  </div>
</template>

<script>
// import testmessage2 from "./personmode/pre_message.vue";
export default {  
  components: {
    // 'per-message':testmessage2,
    'per-message':()=>import("./personmode/pre_message.vue"),
  },
  data() {
    return {
      setnote:'',
      msg_obj:{}
    };
  },
  mounted: function() {
    this.$nextTick(function(ischeck) {
      
    });
    // 这里只是操作dom
    // console.log('验证结果='+res);
    // console.log(this.ischeck.isok);
    // this.$set(this.ischeck, 'isok', '1')
  },
  watch: {    
    // 这里写方法会影响性能
  },
  methods: {
    closeDialog(){
      this.show_msg=false;
    //把绑定的弹窗数组 设为false即可关闭弹窗
    },
    tt:function(){
      console.log('弹窗');
      // 方式1弹出
      // this.setnote='新内容'+(Math.random() * 35);
      // /*
      // 直接 
      // this.$refs.child1.refresh_data();
      // 会导致参数没有传过去,界面已经弹出
      // */ 
      // var that=this;
      // this.$nextTick(function(ischeck) {
      //   that.$refs.child1.refresh_data();
      // });
      // 方式2监听弹出
      this.setnote='监听弹出'+(Math.random() * 35);
    },
    tt_ok:function(){
      this.msg_obj={'showtype':'ok','note':'通过检查'};
    },
    tt_no:function(){
      this.msg_obj={'showtype':'no','note':'检查失败'};
    },
    tt_err:function(){
      this.msg_obj={'showtype':'err','note':'发现错误'};
    },
    tt_warning:function(){
      this.msg_obj={'showtype':'warning','note':'警告。。。。。。。。。'};
    }
    
  }
};

</script>
<style scoped>
</style>

 

转载于:https://my.oschina.net/qingqingdego/blog/3022522

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/101049.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 5种方式实现 Java 单例模式

    5种方式实现 Java 单例模式单例模式(SingletonPattern)是Java中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。是否多线程安全:是是否懒加载:否正如名字含义,饿汉需要直接创建实例。缺点:类加载就初始化,浪费内存优点:没有加锁,执行效率高。还是线程安全的实例。懒汉单例,在类初始化不会创建实例,只有被调用时

    2022年8月11日
    5
  • android 7 uc flash,UC浏览器7.2版新增加FLASH游戏支持功能[通俗易懂]

    android 7 uc flash,UC浏览器7.2版新增加FLASH游戏支持功能[通俗易懂]一直以来,FLASH游戏都是手机的一个硬伤,比如当前令人疯狂的偷菜游戏,一直以来也只能玩纯文字版或者图文版,而不能在手机上实现电脑一样的体验。就在5月19日,国内著名的手机浏览器UC发布了7.2正式版,其革命性的应用了FLASH10技术,使得手机能够完美支持FLASH游戏,彻底改变了开篇所说到的窘境。小编在新版本发布的第一时间,就对7.2正式版进行了“尝鲜”,让我们一起来看看UC浏览器FLASH的…

    2022年6月3日
    62
  • 四大主流CA机构_CA机构的作用

    四大主流CA机构_CA机构的作用四大主流CA机构–wosign是唯一支持免费证书的找到免费SSL证书了,刚刚看到他们网站有快捷申请免费SSL证书,很方便,10分钟颁发,试了一下,申请了2个域名,一个颁发很快,另一个稍微有点慢,问他们客服,客户说另外一个域名,涉及到敏感信息,需要两签,所以审核会慢一下,好吧,只要证书好用,等一会也无所谓啦!另外,我是看到微博上面的这个四大主流CA机构证书对比表,才去的申请的哦!…

    2022年10月31日
    0
  • ajax返回JSON时的处理方式

    ajax返回JSON时的处理方式

    2021年11月4日
    37
  • 平均数,中位数,众数的特点及应用场合图片_中位数众数应用例子

    平均数,中位数,众数的特点及应用场合图片_中位数众数应用例子平均数、中位数、众数都是度量一组数据集中趋势的统计量。所谓集中趋势是指一组数据向某一中心值靠拢的倾向,测度集中趋势就是寻找数据一般水平的代表值或中心值。而这三个特征数又各有特点,能够从不同的角度提供信息。平均数特点:计算用到所有的数据,它能够充分利用数据提供的信息,它具有优的数学性质,因此在实际应用中较为广泛。但它受极端值的影响较大。应用场合:没有极端值的情况下数据集中趋势的刻画。

    2022年9月16日
    0
  • Wireshark安装使用[通俗易懂]

    Wireshark工具下载下载解压默认安装即可然后选择你的网卡点击Start抓包即可

    2022年4月18日
    56

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号