最近项目中遇到需要一个带input输入框的模态框。想找一个框架中自带的,结果没有找到 都是不带输入框的

因为考虑到复用性封装成组件了
父组件调用:

<view @tap="onModalShow">调用模态框<view >

<inputM :modalData='modal' @dataInput='dataInput'></inputM>
<script>
	import inputM from "../../../components/modal/modal.vue"
	export default {
		components:{
			inputM
		},
		data() {
			return {
				modal:{
					showModal: false,
					title:'标题名称',
					text:'input提示内容'
				}
			}
		},
		methods: {
			onModalShow(){
				this.modal.showModal = true
			},
			//   组件中input中传过来的值
			dataInput(res){
				console.log(res)
			}
		}
	}

组件:
HTML部分

<template name="modal">
	<view class="modalInputBg" v-show="modalData.showModal" @tap="modalData.showModal = false">
		<view class="modalInput">
			<view class="modalDiv" @tap.stop="Bubbling">
				<view class="title">企业名称</view>
				<view class="body"><input type="text" v-model="inputVal" value="" placeholder="填写企业名称"/></view>
				<view class="bottom">
					<view class="bottomCancel" @tap.stop="modalData.showModal = false">取消</view>						
					<view class="bottomDefine" @tap.stop="onDefine">确定</view>
				</view>
			</view>
		</view>
	</view>
</template>

js部分

<script>
	export default {
		props: { //此处定义传入的参数
			modalData: {
				type: Object
			}
		},
		data() {
			return {
				inputVal: ''
			}
		},

		methods: {
			onDefine(){
				this.modalData.showModal = false
				this.$emit('dataInput', this.inputVal);
				this.inputVal = ''
			},
			Bubbling(){}
		}
	}
</script>

css样式

<style>
	.modalInputBg{
		position: fixed;
		z-index: 999;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, .2);
	}
	.modalInput{
		width: 100%;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.modalDiv{
		width: 86%;
		height: auto;
		background-color: #FFFFFF;
		border-radius: 10rpx;
	}
	.modalInput .modalDiv .title{
		text-align: center;
		padding: 48rpx 0 0 0;
		letter-spacing: 2rpx;
		font-size: 44rpx;
	}
	.modalInput .modalDiv .body{
		width: 86%;
		height: 80rpx;
		line-height: 80rpx;
		margin: 72rpx auto;
		border: 2rpx solid #DCDEE0;
		box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.08);
		padding: 0 3%;
		box-sizing: border-box;
		border-radius: 10rpx;
	}
	.modalInput .modalDiv .body input{
		height: 100%;
	}
	.modalInput .modalDiv .bottom{
		display: flex;
		align-items: center;
		height: 88rpx;
		border-top: 2rpx solid #DCDEE0;
		box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.08);
	}
	.modalInput .modalDiv .bottom view{
		flex: 1;
		text-align: center;
		font-size: 30rpx;
		letter-spacing: 2rpx;
		height: 100%;
		line-height: 88rpx;
	}
	.modalInput .modalDiv .bottom .bottomCancel{
		border-right: 2rpx solid #DCDEE0;
	}
	.modalInput .modalDiv .bottom .bottomDefine{
		color: #3F83F7;
	}
</style>

效果:
在这里插入图片描述

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐