vue 利用extend 写confirm (小程序无法使用,没办法操作dom)

1 篇文章 0 订阅

组件:index.vue

<template>
	<view  v-if="isShow">
		<view class="z_confirm"></view>
		<view class="tips_box">
			<view class="title ellip_one line_bottom">
				{{title}}
			</view>
			<view class="content line_bottom">
				<input class="uni-input message" v-model="inputData" focus :placeholder="placeholder" />
				
			</view>
			<view class="bottom">
				<zButton size="small" @click='cancel'> 拒 绝 </zButton>
				<zButton type="danger" @click='confirm'> 确 认</zButton>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name:'confirm',
		props: {
			
		},
		data() {
			return {
				title:'标题',
				placeholder:'请输入',
				inputData:'',
				message:'',
				isShow:false,
				promiseStatus:null,
				resolve: '',
				reject: '',
				promise:'',
			}
		},
		mounted() {
			
		},
		methods: {
			// 初始化
			install(){
				let _this = this;
				return new Promise((resolve, reject) => {
					this.resolve = resolve;
					this.reject = reject;
				});
			},
			//取消按钮
			cancel(){
				this.isShow = false
				// this.promiseStatus && this.promiseStatus.resolve();
				this.reject('cancel')
			},
			// 确认按钮
			confirm(){
				this.isShow = false
				this.resolve(this.inputData)
				// this.promiseStatus && this.promiseStatus.resolve();
			},
		}
	}
</script>

<style lang="scss">
		.z_confirm {
			display: block;
			width: 100%;
			position: fixed;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
			background-color: #000000;
			opacity: 0.4;
			filter: alpha(opacity=40);
			}
			.tips_box{
					position: fixed;
					left: 10%;
					top: 40%;
					width: 80%;
					height: auto;
					margin-left: -20upx;
					border-radius: 6upx;
					padding:20upx;
					background-color:#fff;
					opacity: 1;
					filter: alpha(opacity=100);
					.title{
						font-size: 30upx;
						height: 80upx ;
						line-height: 60upx;
						font-weight: 700;
						text-align: center;
					}
					.content{
						padding: 30upx 0;
						.message{
							border-radius: 8upx;
							background: #F1F1F1;
							padding: 10upx;
							
						}
					}
					.bottom{
						padding: 40upx 0 0;
						text-align: center;
						.z_button{
							width: 40%;
							margin: 0 5%;
							// font-size: 30upx;
							padding: 20upx 0;
						}
					}
				}
			
</style>

index.js

import Vue from 'vue'
var zConfirm = () => {
	let zConfirmBox = Vue.extend(require('./index.vue').default)
	let instance = new zConfirmBox();
	instance.$mount();
	let dom = document &&  document.body ? document.body : wx.createSelectorQuery().select('view')
	dom.appendChild(instance.$el);
	
	
	Vue.prototype.$zConfirm = ({title,placeholder}) =>{
		instance.isShow = true
		instance.placeholder = placeholder
		instance.title = title
		return instance.install()
		.then(val => {
			return Promise.resolve(val);
		})
		.catch(err =>{
			return Promise.reject(err);
		})
		
	}
	
}


export default zConfirm

main.js

import zConfirm from './components/elementBox/toast/index.js';
Vue.use(zConfirm);

使用:

this.$zConfirm({
	title:'标题',
		content:'CCC',
		placeholder:'请输入'
		
	}).then(( val ) => {
		console.log('确认')
	}).catch((err)=>{
		console.log('取消')
	})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值