步骤一

创建一个弹窗页面,我们给该页面命名为dialogComponent,弹窗页面中要设置以下内容:

<template>
<!--1.首先,弹窗页面中要有el-dialog组件即弹窗组件,我们把弹窗中的内容放在el-dialog组件中-->
<!--2.设置:visible.sync属性,动态绑定一个布尔值,通过这个属性来控制弹窗是否弹出-->
  <el-dialog title="弹窗" :visible.sync="detailVisible" width="35%">
    弹窗内容
  </el-dialog>
</template>

<script>
    export default {
        name: "dialogComponent",
        data(){
          return{
            detailVisible:false
          }
        },
      methods:{
      //3.定义一个init函数,通过设置detailVisible值为true来让弹窗弹出,这个函数会在父组件的方法中被调用
        init(data){
          this.detailVisible=true;
          //data是父组件弹窗传递过来的值,我们可以打印看看
          console.log(data);
        }
      }
    }
</script>

步骤二

在父组件中引入弹窗组件,并通过点击事件弹出弹窗,父组件主要设置以下内容:

<template>
	<!-- 6.定义一个点击事件-->
	<button @click="handleClick('父组件')">点击</button>
	<!-- 3.在页面中使用dialog-component组件-->
	<!-- 4.设置v-if语句,通过动态改变Visiable值用来控制弹窗是否弹出-->
	<!-- 5.设置ref属性,相当于唯一标识dialog-component组件-->
	<dialog-component v-if="Visiable" ref="dialog"></dialog-component>
</template>

<script>
//	1.引入弹窗组件dialogComponent
import dialogComponent from "./dialogComponent";
    export default {
    //  2.在components中注册dialogComponent组件
    	components:{
          dialogComponent
      },
        data(){
          return{
            Visiable:false
          }
        },
      methods:{
      // 7.实现点击事件,点击事件一定要包含以下内容
      	handleClick(data){
          this.Visiable=true;
          this.$nextTick(()=>{
          //这里的dialog与上面dialog-component组件里面的ref属性值是一致的
          //init调用的是dialog-component组件里面的init方法
          //data是传递给弹窗页面的值
            this.$refs.dialog.init(data);
          })
        },
      }
    }
</script>

注:vue组件在定义的时候使用驼峰命名,但是在使用的时候要转化为短横线命名!

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐