iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >vue弹窗父子组件调用问题示例详解
  • 200
分享到

vue弹窗父子组件调用问题示例详解

2024-04-02 19:04:59 200人浏览 安东尼
摘要

目录一、Vue弹窗 父子组件 emit 传图片二、vue父组件调用子组件里的不同方法一、vue弹窗 父子组件 emit 传图片 1、:modal-append-to-body=&qu

一、vue弹窗 父子组件 emit 传图片

1、:modal-append-to-body="false"为了解决element ui中引入dialog窗口组件后遮罩层会挡住dialog窗口的用处,默认为true,改为false即可解决。

2、此弹窗主要为了解决收到下位机急停信号后,上位机前台显示弹窗的重复性。

//此为子组件(customComponents.vue)
<div>
    <el-dialog 
        :visible.sync="dialogVisible" 
        width="25%" 
        :modal-append-to-body="false">
 
		<div slot="title" class="dialog-header-title">
		   <img  :src="url" >
		   <span> 提示</span>
		</div>
		<span>{{this.message}}</span>
	    <span slot="footer" class="dialog-footer">
		    <el-button 
                type="primary" 
                size="small" 
                @click="dialogVisible = false" >确 定 </el-button>
		</span>
 
	</el-dialog>
</div>
<script>
	export default {
		props: {
			url: String,//String为定义参数类型,例如图片地址就是String类型的
			message: String,//String为定义参数类型
		},
	    data() {
	      return {
	        dialogVisible: true,
	      };
	    },
	}
</script>
//此为父组件(treatmentInterface.vue)
<customComponents 
    v-if="empStopStatus== 0" 
        :url="iconDanger" 
            :message='messageDanger' ></customComponents>
<div v-else></div>
<script>
    //引入局部组件(子组件)
	import customComponents from "../customComponents/customComponents"
    
    export default {
	    name: " ",
	    components: {customComponents},
        data() {
	      return {
			iconDanger: require('../../assets/icons/customComponents/danger.png'),
			messageDanger: "急停按钮被按下!请先检查设备...",
	      };
	    },
	}
</script>

二、vue父组件调用子组件里的不同方法

1、vue的动态方法绑定

主要看:

①:<el-button>标签里的@click

②:methods里面的buttonClick(methodName)

//此为子组件(customComponents.vue)
<div>
    <el-dialog 
        :visible.sync="dialogVisible" 
        width="25%" 
        :modal-append-to-body="false">
 
		<div slot="title" class="dialog-header-title">
		   <img  :src="url" >
		   <span> 提示</span>
		</div>
		<span>{{this.message}}</span>
	    <span slot="footer" class="dialog-footer">
		    <el-button 
                type="primary" 
                    size="small" 
                        @click="buttonClick(methodsName)">确 定</el-button>
		</span>
 
	</el-dialog>
</div>
<script>
	export default {
		props: {
			url: String,//String为定义参数类型,例如图片地址就是String类型的
			message: String,//String为定义参数类型
		},
	    data() {
	      return {
	        dialogVisible: true,
	      };
	    },
        methods:{
            buttonClick(methodName) {
				this[methodName]()
			},
            treatFinished() {
				console.log("执行了..........")
			},
			emergencyStop() {
				this.dialogVisible = false
			}
        },
	}
</script>

至此,已完成子组件里不同方法的绑定,下一步就要在父组件里调用上方子组件里的方法了。

2、父组件调用子组件方法

 //此为父组件(treatmentInterface.vue)
<!-- 治疗完成弹窗 -->
<customComponents
    v-if="treatStatus== 1" 
        :url="iconDone" 
            :message='messageFinished' 
                :methodsName='treatFinished'></customComponents>
<div v-else></div>
<!-- 急停被按下弹窗 -->
<customComponents 
v-if="empStopStatus== 0" 
    :url="iconDanger" 
        :message='messageDanger' 
            :methodsName='emergencyStop'></customComponents>
<div v-else></div>
<script>
    //引入局部组件(子组件)
	import customComponents from "../customComponents/customComponents"
    
    export default {
	    components: {customComponents},
        data() {
	      return {
			iconDanger: require('../../assets/icons/customComponents/danger.png'),
			messageDanger: "急停按钮被按下!请先检查设备...",
            treatFinished: 'treatFinished',
			emergencyStop: 'emergencyStop',
	      };
	    },
	}
</script>

到此这篇关于vue弹窗父子组件调用的文章就介绍到这了,更多相关vue父子组件调用内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: vue弹窗父子组件调用问题示例详解

本文链接: https://www.lsjlt.com/news/165968.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作