Vue ElementUI this.$confirm async await封装方式

 更新时间:2022年09月23日 15:47:22   作者:asdfsdgfsdgfa  
这篇文章主要介绍了Vue ElementUI this.$confirm async await封装方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Vue ElementUI this.$confirm async await封装

this.$confirm官网:

https://element.eleme.cn/#/zh-CN/component/message-box

改造前

    async test() {
      console.log("111111");
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          console.log("点击确定");
 
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          console.log("点击取消");
 
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      console.log("2222222");
    },

async await改造后

async test() {
      console.log("111111");
      let confirmRes = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch(() => {});
 
      if (confirmRes !== "confirm") {
        console.log("点击取消");
        return;
      }
      console.log("点击确定");
      console.log("2222222");
    }

一定要加上.catch(() => {});否则报错

Vue elementUI组件封装思路

核心

父子传值、slot插槽

插槽传值

<slot title=“123” name=“aaa”></slot>

父组件接收插槽值

<div slot=“aaa” slot-scope=“props” :value=“props.title”></div>

示例步骤

1、在components文件夹下新建一个MyComponent1文件夹,新建MyCompont1.vue

(以btn为例)

<template>
  <el-button-group>
    <el-button 
        v-for="(btn,index) in this.buttons" 
        :key="index" 
        :type="btn.type ? btn.type:'primary'"
        :icon="btn.icon" 
        :size="btn.size?btn.size:'mini'"
        @click="btn.click"
    >
        {{btn.label}}
    </el-button>
  </el-button-group>
</template>
<script>
export default {
  name: 'MyComponent1', // name就是封装好后使用的组件名
  props: {
    buttons: {
      type: Array,
      required: true
    }
  }
}
</script>

2、components文件夹下新建index.js, 用于注册组件,也可以在main.js中注册,为了统一管理建议放在components中注册

3、然后main.js中引入,就可以直接使用了**

注册

import Vue from 'vue'
import MyComponent1 from './MyComponent1/index.vue'
//多个组件就多次注册
Vue.component(MyComponent1.name, MyComponent1)
''

使用

<template>
  <div>
    <MyComponent1 :buttons="buttons"></MyComponent1>
  </div>
</template>
<script>
export default {
  name: '',
  data () {
    return {
      buttons: [{
        label:'创建',
        icon:'el-icon-circle-plus-outline',
        click: ()=>{console.log('创建')}
      },{
        label:'修改',
        icon:'el-icon-edit-outline',
        click: ()=>{console.log('修改')}
      }]
    }
  }
}
</script>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Vue2.0实现自适应分辨率

    Vue2.0实现自适应分辨率

    这篇文章主要为大家详细介绍了Vue2.0实现自适应分辨率,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • 在vue项目实现一个ctrl+f的搜索功能

    在vue项目实现一个ctrl+f的搜索功能

    刚刚接到领导通知,需要实现搜索功能,因为项目是vue的而且是手机端,对我来说有点小难度。经过小编的一番思索最终还是解决了,今天小编把实现过程分享到脚本之家平台,需要的朋友参考下
    2020-02-02
  • Vue-Router滚动行为的具体使用

    Vue-Router滚动行为的具体使用

    在 Vue Router 中,你可以使用滚动行为来定义路由切换时页面滚动的行为,本文就详细的介绍一下Vue-Router滚动行为的具体使用,感兴趣的可以了解一下
    2023-08-08
  • 基于Vue设计实现一个弹幕组件

    基于Vue设计实现一个弹幕组件

    这篇文章主要给大家分享一个开发中常见的需求,接下来将为大家详细介绍弹幕的实现以及设计思路一步一步描述出来,希望大家能够喜欢
    2023-06-06
  • Vue Router的手写实现方法实现

    Vue Router的手写实现方法实现

    这篇文章主要介绍了Vue Router的手写实现方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • 详解为什么Vue中的v-if和v-for不建议一起用

    详解为什么Vue中的v-if和v-for不建议一起用

    这篇文章主要介绍了面试官:为什么Vue中的v-if和v-for不建议一起用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • vue调用微信JSDK 扫一扫,相册等需要注意的事项

    vue调用微信JSDK 扫一扫,相册等需要注意的事项

    这篇文章主要介绍了vue调用微信JSDK 扫一扫,相册等需要注意的事项,帮助大家更好的理解和使用vue框架,感兴趣的朋友可以了解下
    2021-01-01
  • vue弹窗组件使用方法

    vue弹窗组件使用方法

    弹窗是一个项目必备的复用利器,这篇文章主要介绍了vue弹窗组件的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • 在vue中使用G2图表的示例代码

    在vue中使用G2图表的示例代码

    这篇文章主要介绍了在vue中使用G2图表的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-03-03
  • 在vue-cli搭建的项目中增加后台mock接口的方法

    在vue-cli搭建的项目中增加后台mock接口的方法

    这篇文章主要介绍了在vue-cli搭建的项目中增加后台mock接口的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04

最新评论