vue 父组件调用子组件中的方法

  • Post author:
  • Post category:vue


子组件

<template>
	<view class="m-toast" :class="isShowToast ? 'd-flex' : ''">
		<view class="m-toast-content">
			<image src="../../static/icon-toast.png"></image>
			<view>{{title}}</view>
		</view>
	</view>
</template>

<script>
	export default {
		name:"toast",
		props: {
			icon: {
				type: String,
				default:'error'
			},
			title: {
				type: String,
				default:'请设置标题'
			},
		},
		data() {
			return {
				isShowToast: false
			};
		},
		methods: {
			toastMethod() {
				let _this = this;
				_this.isShowToast = true;
				setTimeout(function() {
					_this.isShowToast = false;
				}, 5000);
			}
		}
	}
</script>

<style>
	.m-toast {
		position: fixed;
		top: 0;
		right: 0;
		left: 0;
		bottom: 0;
		display: none;
		align-items: center;
		justify-content: center;
	}
	.d-flex {
		display: flex !important;
	}
	.m-toast-content {
		text-align: center;
		width: 300rpx;
		max-height: 100%;
		background-color: rgba(17,17,17,.7);
		border-radius: 10px;
	} 
	.m-toast-content image {
		margin: 20rpx 0 8rpx;
		width: 80rpx;
		height: 80rpx;
	}
	.m-toast-content view {
		margin-bottom: 10px;
		padding: 0 10rpx;
		font-size: 30rpx;
		color: #fff;
	}
</style>

父组件

<template>
	</view>
		<toast title="该填报人审核内容不清晰,请重新填报!" ref="toast"></toast>
	</view>
</template>
<script>
	import toast from "@/components/toast/toast.vue"; // 调用子组件 toast
	export default {
		components: {
			toast
		},
		methods: {
			tapPopup(e) {
				this.$refs.toast.toastMethod(); // 调用子组件的方法
			}
		}
	}
</script>



版权声明:本文为qq_34720531原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。