Vue ElementUI this.$confirm async await封裝方式
Vue ElementUI this.$confirm async await封裝
this.$confirm官網(wǎng):
https://element.eleme.cn/#/zh-CN/component/message-box
改造前
? ? async test() {
? ? ? console.log("111111");
? ? ? this.$confirm("此操作將永久刪除該文件, 是否繼續(xù)?", "提示", {
? ? ? ? confirmButtonText: "確定",
? ? ? ? cancelButtonText: "取消",
? ? ? ? type: "warning",
? ? ? })
? ? ? ? .then(() => {
? ? ? ? ? console.log("點(diǎn)擊確定");
?
? ? ? ? ? this.$message({
? ? ? ? ? ? type: "success",
? ? ? ? ? ? message: "刪除成功!",
? ? ? ? ? });
? ? ? ? })
? ? ? ? .catch(() => {
? ? ? ? ? console.log("點(diǎn)擊取消");
?
? ? ? ? ? this.$message({
? ? ? ? ? ? type: "info",
? ? ? ? ? ? message: "已取消刪除",
? ? ? ? ? });
? ? ? ? });
? ? ? console.log("2222222");
? ? },async await改造后
async test() {
? ? ? console.log("111111");
? ? ? let confirmRes = await this.$confirm(
? ? ? ? "此操作將永久刪除該文件, 是否繼續(xù)?",
? ? ? ? "提示",
? ? ? ? {
? ? ? ? ? confirmButtonText: "確定",
? ? ? ? ? cancelButtonText: "取消",
? ? ? ? ? type: "warning",
? ? ? ? }
? ? ? ).catch(() => {});
?
? ? ? if (confirmRes !== "confirm") {
? ? ? ? console.log("點(diǎn)擊取消");
? ? ? ? return;
? ? ? }
? ? ? console.log("點(diǎn)擊確定");
? ? ? console.log("2222222");
? ? }一定要加上.catch(() => {});否則報(bào)錯(cuò)
Vue elementUI組件封裝思路
核心
父子傳值、slot插槽
插槽傳值
<slot title=“123” name=“aaa”></slot>
父組件接收插槽值
<div slot=“aaa” slot-scope=“props” :value=“props.title”></div>
示例步驟
1、在components文件夾下新建一個(gè)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, 用于注冊(cè)組件,也可以在main.js中注冊(cè),為了統(tǒng)一管理建議放在components中注冊(cè)
3、然后main.js中引入,就可以直接使用了**
注冊(cè)
import Vue from 'vue' import MyComponent1 from './MyComponent1/index.vue' //多個(gè)組件就多次注冊(cè) Vue.component(MyComponent1.name, MyComponent1) ''
使用
<template> ? <div> ? ? <MyComponent1 :buttons="buttons"></MyComponent1> ? </div> </template>
<script>
export default {
? name: '',
? data () {
? ? return {
? ? ? buttons: [{
? ? ? ? label:'創(chuàng)建',
? ? ? ? icon:'el-icon-circle-plus-outline',
? ? ? ? click: ()=>{console.log('創(chuàng)建')}
? ? ? },{
? ? ? ? label:'修改',
? ? ? ? icon:'el-icon-edit-outline',
? ? ? ? click: ()=>{console.log('修改')}
? ? ? }]
? ? }
? }
}
</script>以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue2.0實(shí)現(xiàn)自適應(yīng)分辨率
這篇文章主要為大家詳細(xì)介紹了Vue2.0實(shí)現(xiàn)自適應(yīng)分辨率,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
在vue項(xiàng)目實(shí)現(xiàn)一個(gè)ctrl+f的搜索功能
剛剛接到領(lǐng)導(dǎo)通知,需要實(shí)現(xiàn)搜索功能,因?yàn)轫?xiàng)目是vue的而且是手機(jī)端,對(duì)我來(lái)說(shuō)有點(diǎn)小難度。經(jīng)過(guò)小編的一番思索最終還是解決了,今天小編把實(shí)現(xiàn)過(guò)程分享到腳本之家平臺(tái),需要的朋友參考下2020-02-02
基于Vue設(shè)計(jì)實(shí)現(xiàn)一個(gè)彈幕組件
這篇文章主要給大家分享一個(gè)開(kāi)發(fā)中常見(jiàn)的需求,接下來(lái)將為大家詳細(xì)介紹彈幕的實(shí)現(xiàn)以及設(shè)計(jì)思路一步一步描述出來(lái),希望大家能夠喜歡2023-06-06
Vue Router的手寫實(shí)現(xiàn)方法實(shí)現(xiàn)
這篇文章主要介紹了Vue Router的手寫實(shí)現(xiàn)方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
vue調(diào)用微信JSDK 掃一掃,相冊(cè)等需要注意的事項(xiàng)
這篇文章主要介紹了vue調(diào)用微信JSDK 掃一掃,相冊(cè)等需要注意的事項(xiàng),幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2021-01-01
在vue-cli搭建的項(xiàng)目中增加后臺(tái)mock接口的方法
這篇文章主要介紹了在vue-cli搭建的項(xiàng)目中增加后臺(tái)mock接口的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04

