美文网首页
弹窗设置(父子传参原理)

弹窗设置(父子传参原理)

作者: TA鸣 | 来源:发表于2019-03-27 18:45 被阅读0次

父组件:

1.父组件向子组件传递数据
父组件绑定属性,给子组件传递数据
子组件通过props接收父组件传递过来的数据
子组件向父组件传递数据

2.父组件自定义事件,并绑定函数
子组件使用$emit触发父组件的自定义事件,并带上数据

<template>
  <div>
    <Dialog :msg="msg" :show="show" @yes="yes" @no="no"/>
    <p>
      <button @click="showDialog">显示弹框</button>
    </p>
  </div>
</template>

<script>
import Dialog from "@/components/Dialog";
import { constants } from "fs";
export default {
  data() {
    return {
      msg: "你好吗",
      show: false
    };
  },
  components: {
    Dialog
  },

  methods: {
    showDialog() {
      this.show = true;
    },
    yes(data) {
      // 关闭弹窗
      this.show = false;
      alert("你点击了确定");
    },
    no(data) {
      // 关闭弹窗
      this.show = false;
      // todo
      alert("你点击了取消");
    }
  }
};
</script>

<style>
</style>

子组件:

<template>
  <div v-show="show">
    <!-- 蒙层 -->
    <div class="pop"></div>
    <div class="box">
      <h3>温馨提示</h3>
      <div class="content">{{msg}}</div>
      <p>
        <button @click="ok">确认</button>
        <button @click="cancel">取消</button>
      </p>
    </div>
  </div>
</template>

<script>
export default {
  props: ["msg", "show"],

  methods: {
    ok() {
      this.$emit("yes", "ok");
    },
    cancel() {
      this.$emit("no", "cancel");
    }
  }
};
</script>

<style scoped>
.pop {
  position: fixed;
  background: #000;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0.5;
}

.box {
  display: inline-block;
  /* width: 300px; */
  /* height: 200px; */
  position: fixed;
  top: 50%;
  margin-left: -150px;
  margin-top: -100px;
  left: 50%;
  z-index: 2;
  background: #fff;
  padding: 50px 100px;
}
</style>

相关文章

  • 弹窗设置(父子传参原理)

    父组件: 1.父组件向子组件传递数据父组件绑定属性,给子组件传递数据子组件通过props接收父组件传递过来的数据子...

  • router-view 父子传参

    导语: 习惯了父子组件传参,今天聊一聊router-view传参。其实本质也是父子组件传参,方法一模一样,之前也提...

  • 父子传参

    在react中父子组件传参一 、父传子 子组件把值传给父组件在父组件中 其实可以把子组件里的方法用箭头函数,这样就...

  • 父子传参

  • 父子传参

    父组件 父组件向子组件传递数据 1.父组件绑定属性,给子组件传递数据2.子组件通过props接收父组件传递过来的数...

  • Flutter 父子组件传参 之 父组件向StatefulWid

    Flutter 父子组件传参 之 父组件向StatefulWidget有状态子组件传参https://www.we...

  • 父子传参(组件)

    第一种,父子组件通信 一.父组件向子组件传值 创建子组件,在src/components/文件夹下新建一个Chil...

  • Vue实战第二天

    路由组件传参 动态路由传参 静态路由传参 函数传参htm5 history 模式 设置通用路由,找不到页面跳转自定...

  • 在vue中使用sync修饰符进行父子组件双向绑定

    今天在使用Vue进行父子组件传值的时候,需要父组件向子组件传值,同时子组件需要关闭父组件的弹窗,这就需要父子组件的...

  • query与params的页面传值

    先简单记录页面传值, 还没完全弄懂其中的原理先配置路径跳转 params data传参 params传参 跳转页面...

网友评论

      本文标题:弹窗设置(父子传参原理)

      本文链接:https://www.haomeiwen.com/subject/acezvqtx.html