欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue實現原理this.$message實例詳解

 更新時間:2024年03月08日 09:45:03   作者:Hhua.  
這篇文章主要介紹了vue實現原理this.$message實例詳解,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧

vue實現原理this.$message

components 通用組件文件夾下 創(chuàng)建 esConfirm文件夾

創(chuàng)建 index.js js文件

import Vue from 'vue';
import Confirm from './index.vue'; // 引入組件
let newInstance;
const ConfirmInstance = Vue.extend(Confirm); // 創(chuàng)建構造函數
const initInstance = () => { // 執(zhí)行方法后完成掛載
	newInstance = new ConfirmInstance(); // 實例化
	document.body.appendChild(newInstance.$mount().$el);
// 實例化后手動掛載,得到$el真實Dom,將其添加到body最后
}
export default options => {
//導出一個方法,接受配置參數
if (!newInstance) {
	initInstance(); // 掛載
}
Object.assign(newInstance, options);
// 實例化后newInstance就是一個對象了,所以data內的數據會
// 掛載到this下,傳入一個對象與之合并
return newInstance.show(vm => { // 顯示彈窗
	newInstance = null; // 將實例對象清空
})
}

esConfirm文件夾下創(chuàng)建組件 index.vue

<template>
  <!-- 二次確認彈窗 -->
  <el-dialog width="440px" class="delete-confirm" :visible.sync="confirmDialigShow" :show-close="false"
    :close-on-click-modal="false">
    <div class="top">
      <img class="danger" :src="require('./danger.png')">
      <div class="title">{{ title }}</div>
    </div>
    <div class="bottom">{{ content }}</div>
    <span slot="footer" class="dialog-footer">
      <el-button v-if="showCancelButton" size="small" @click="cancel">{{ cancelButtonText || '取消' }}</el-button>
      <el-button size="small" type="primary" @click="confirm">{{ confirmButtonText || '確定' }}</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  data() {
    return {
      title: '提示',
      content: '該數據刪除將無法恢復,是否確認刪除?',
      confirmButtonText: '確定',
      cancelButtonText: '取消',
      showCancelButton: true,
      confirmDialigShow: true
    }
  },
  methods: {
    show(cb) {
      this.showFlag = true
      typeof cb === "function" && cb.call(this, this)
      return new Promise((resolve, reject) => {
        this.reject = reject
        this.resolve = resolve
      })
    },
    cancel() {
      this.reject("cancel")
      this.hide()
    },
    confirm() {
      this.resolve("confirm")
      this.hide()
    },
    hide() {
      this.showFlag = false
      document.body.removeChild(this.$el)
      this.$destroy()
    }
  }
}
</script>
<style lang='scss' scoped>
.delete-confirm {
  // width: 440px;
  box-shadow: 0px 4px 12px 0px rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  .top {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
    .danger {
      width: 30px;
      height: 24px;
      margin-right: 12px;
    }
    .title {
      font-weight: 500;
      font-size: 16px;
      color: rgba(0, 0, 0, 0.85);
      line-height: 24px;
    }
  }
  .bottom {
    font-size: 14px;
    font-weight: 400;
    color: #324457;
    line-height: 22px;
    margin-top: 20px;
  }
  ::v-deep .el-dialog__header {
    display: none;
  }
  ::v-deep {
    .el-dialog {
      margin-top: 20vh !important;
    }
    .el-dialog__body {
      padding: 24px;
    }
    .el-dialog__footer {
      border: none;
      line-height: 0;
      padding: 8px 24px 24px 0 !important;
    }
  }
}
</style>

在main.js 注冊組件

import esConfirm from '@/components/esConfirm/index.js'
Vue.prototype.$esConfirm = esConfirm

調用

this.$esConfirm({
title: '是否刪除?'
}).then(res => {
console.log(res)
}).catch(error => {
console.log(error)
})

Vue中的信息提示this.$message方法的使用

代碼:

                    this.$message({
                        message:'保存成功了,我是message',
                        type: 'success'
                    })

效果:

在這里插入圖片描述

type的其他參數:
success(成功) 、warning(警告)、info(消息)、error(錯誤)

可以在請求成功之后執(zhí)行:

axios.post("http://localhost:3312/user/save",this.form).then((resp)=>{
                let data = resp.data
                if(data.success){
                   ......
                    this.$message({
                        message:'保存成功了,我是message',
                        type: 'success'
                    })
                }
         })

到此這篇關于vue實現原理this.$message的文章就介紹到這了,更多相關vue this.$message內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 詳解vue-amap引入高德JS API的原理

    詳解vue-amap引入高德JS API的原理

    vue-amap是對高德地圖JS API進行封裝的、適用于vue項目的地圖組件庫,本文主要介紹了vue-amap引入高德JS API的原理,具有一定的參考價值,感興趣的可以了解一下
    2022-06-06
  • Vue3中依賴注入provide、inject的使用

    Vue3中依賴注入provide、inject的使用

    這篇文章主要介紹了Vue3中依賴注入provide、inject的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Vite代理如何解決跨域問題詳解

    Vite代理如何解決跨域問題詳解

    跨域是指瀏覽器不能執(zhí)行其他網站的腳本,它是由瀏覽器的同源策略造成的,是瀏覽器對JavaScript實施的安全限制,下面這篇文章主要給大家介紹了關于Vite代理如何解決跨域問題的相關資料,需要的朋友可以參考下
    2023-03-03
  • Vue路由權限控制解析

    Vue路由權限控制解析

    這篇文章主要介紹了Vue路由權限控制的相關資料,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2020-11-11
  • vue3如何使用postcss-px-to-viewport適配屏幕

    vue3如何使用postcss-px-to-viewport適配屏幕

    這篇文章主要介紹了vue3如何使用postcss-px-to-viewport適配屏幕問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Vue中created和mounted使用場景分析

    Vue中created和mounted使用場景分析

    vue.js中created方法是一個生命周期鉤子函數,一般可以在created函數中調用ajax獲取頁面初始化所需的數據,這篇文章主要介紹了Vue中created和mounted使用場景分析,需要的朋友可以參考下
    2023-05-05
  • vue使用節(jié)流函數的踩坑實例指南

    vue使用節(jié)流函數的踩坑實例指南

    防抖和節(jié)流的目的都是為了減少不必要的計算,下面這篇文章主要給大家介紹了關于vue使用節(jié)流函數踩坑的相關資料,需要的朋友可以參考下
    2021-05-05
  • axios取消請求與避免重復請求

    axios取消請求與避免重復請求

    在項目中經常有一些場景會連續(xù)發(fā)送多個請求,而異步會導致最后得到的結果不是我們想要的,并且對性能也有非常大的影響,這篇文章主要給大家介紹了關于axios取消請求與避免重復請求的相關資料,需要的朋友可以參考下
    2021-06-06
  • Vue電商網站首頁內容吸頂功能實現過程

    Vue電商網站首頁內容吸頂功能實現過程

    電商網站的首頁內容會比較多,頁面比較長,為了能讓用戶在滾動瀏覽內容的過程中都能夠快速的切換到其它分類。需要分類導航一直可見,所以需要一個吸頂導航的效果。目標:完成頭部組件吸頂效果的實現
    2023-04-04
  • vue實現登陸功能

    vue實現登陸功能

    這篇文章主要為大家詳細介紹了vue實現登陸功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03

最新評論