Element的$prompt方法的使用及自定义显示内容

自定义显示内容效果图:

在这里插入图片描述

直接上代码:

func(row){
	const h = this.$createElement
	const form = this.form
	// 自定义组件模板
    const CustomInputComponent = {
      data() {
        return {
          inputValue: '',
          row,//如果模板中要使用方法中的变量,需要先在data中声明才能使用
          form,
          rule: {
            inputValue: [
              {
                required: true,
                message: '请输入原因',
                trigger: 'blur',
              },
            ],
          },
          model: {
            inputValue: '',
          },
        }
      },
      template: `
      <div>
      	<el-form label-position="top" :model="model" :rules="rule" ref="formaa">
      		<el-form-item label='原因:' prop="inputValue" >
      			<el-input v-model="model.inputValue" placeholder="请输入" maxlength="15"></el-input>
      		</el-form-item>
      	</el-form>
        <p>编号:<el-link type="primary">{{form.name}}</el-link></p>
      </div>
    `,
    }
    
    // 创建自定义组件实例
	const CustomInputInstance = h(CustomInputComponent)
	//获取自定义模板中的值
	let inpVal = CustomInputInstance.componentInstance.inputValue
	// 显示弹框
    this.$prompt(null, '提示', {
       confirmButtonText: '确定',
       cancelButtonText: '取消',
       showInput: false,//隐藏原有的input
       dangerouslyUseHTMLString: true,
       message: CustomInputInstance,//创建的自定义组件实例
       beforeClose: (action, instance, done) => {
         //点击确定按钮时获取输入框的值
         if (action == 'confirm') {
         	//触发表单校验
         	CustomInputInstance.componentInstance.$refs.formaa.validate((valid) => {
       			if (valid) {
       				//此处写表单校验通过后的业务
       				done()
       			}
         	})
         } else {
           done()
         }
       },
     })
       .then(() => {
         console.log('Input value:', inpVal)
       })
       .catch(() => {
         console.log('Prompt cancelled')
       })
	
},

其中beforeClose中的done()方法是继续关闭弹窗的作用,不触发则不会关闭弹窗

默认显示效果图

在这里插入图片描述
代码如下:

funC(){
	this.$prompt('请输入id', '提示', {
      confirmButtonText: '确定',
        cancelButtonText: '取消',
      })
        .then(({ value }) => {
          console.log('输入框输入的值:',value)
        })
        .catch(() => {})
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值