vue通过点击事件d出d窗页面

vue通过点击事件d出d窗页面,第1张

步骤一

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

<template>


  <el-dialog title="d窗" :visible.sync="detailVisible" width="35%">
    d窗内容
  el-dialog>
template>

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

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

<template>
	
	<button @click="handleClick('父组件')">点击button>
	
	
	
	<dialog-component v-if="Visiable" ref="dialog">dialog-component>
template>

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/945783.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-18
下一篇 2022-05-18

发表评论

登录后才能评论

评论列表(0条)

保存