Vue消息提示this.$message方法使用解讀
更新時間:2023年09月26日 08:36:22 作者:老李四
這篇文章主要介紹了Vue消息提示this.$message方法使用,具有很好的參考價值,希望對大家有所幫助,
Vue消息提示this.$message方法
HTML
<el-button @click="saveData">彈窗</el-button>
JavaScript
saveData(){ ? ? this.$message({undefined ? ? ? ? message:“這是彈框消息”, ? ? ? ? type:‘success' ? ? ?}) ?// type 取值 ‘success'(成功) /warning(警告)/info(消息)/error(錯誤)/ }
element 的 this.$message( ) 消息提示實現
在vue項目中,直接通過js代碼 this.$message( )就可以調出消息提示組件,這是如何實現的呢
主要分為以下幾步
1.用 Vue.extend 創(chuàng)建組件的模板(構造函數)
2.創(chuàng)建一個函數,在函數內部, 實例化組件并進行掛載到相應元素上
3.將創(chuàng)建的這個函數綁定到Vue原型上,就可以通過this .訪問了
上代碼,
如下目錄結構
在main.js中
import Vue from "vue"; import message from "./main.vue"; // 1.用 Vue.extend 創(chuàng)建組件的模板(構造函數) let messageConstructor = Vue.extend(message); let instance; const Message = function(options) {// options是傳入的參數配置 {message: '成功',type: "success"offset: 80} // 2.實例化組件 instance = new messageConstructor({ data: options }); // 把options導入data中 // 3.組件掛載 instance.$mount(); document.body.appendChild(instance.$el); // 設置offset let verticalOffset = options.offset || 20; instance.verticalOffset = verticalOffset; instance.duration = options.duration || 3000; return instance; }; export default Message;
在main.vue中
<template> <div v-show="visible" :class="['my-message', 'my-message-' + type, center ? 'is-center' : '']" :style="positionStyle" > <slot> <p>{{ message }}</p> </slot> <i v-if="showClose" class="el-icon-close" @click="close"></i> </div> </template> <script> export default { name: "Message", data() { return { visible:false, verticalOffset: 20, duration: 3000, timer: null, center: false, showClose: false, closed: false, }; }, mounted() { this.autoClose(); }, beforeDestroy() { clearTimeout(this.timer); }, watch: { closed(newVal) { if (newVal) { this.visible = false; } } }, computed: { positionStyle() { return { top: `${this.verticalOffset}px`, }; }, }, methods: { autoClose() { this.timer = setTimeout(() => { this.$destroy(true); this.$el.parentNode.removeChild(this.$el); }, this.duration); }, close() { this.closed = true; clearTimeout(this.timer); } }, }; </script> <style> .my-message { min-width: 380px; border: 1px solid #ebeef5; border-radius: 4px; position: fixed; left: 50%; top: 20px; transform: translateX(-50%); background-color: #edf2fc; transition: opacity 0.3s, transform 0.4s, top 0.4s; overflow: hidden; display: flex; align-items: center; padding: 0 15px; } p { } .my-message-info { color: #909399; } .my-message-success { background: #f2f9ec; color: #67c23a; border-color: #e4f2da; } .my-message-warning { background: #fcf6ed; color: #e6a23c; border-color: #f8ecda; } .my-message-error { background: #fcf0f0; color: #f56c6c; border-color: #f9e3e2; } .is-center { justify-content: center; } </style>
在index.js中
import Vue from "vue"; import Message from './src/main' Vue.prototype.$message = Message
ok了,大晚上的有點困,有些地方有些潦草,有時間再改下。。。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue3 element-plus二次封裝組件系列之伸縮菜單制作
這篇文章主要介紹了vue3 element-plus二次封裝組件系列之伸縮菜單制作,是基于vue3 vite element-plus搭建的,值的注意的時候,里面的圖標組件是經過處理的,結合實例代碼介紹的非常詳細,需要的朋友可以參考下2023-01-01VUE2舊項目重新安裝依賴后@vue/compiler-sfc編譯報錯問題
在VUE2舊項目中重新安裝依賴后,如果遇到@vue/compiler-sfc編譯報錯,首先需要檢查package.json文件中的Vue版本是否升級到2.7版本,2.7版本的編譯插件不再支持/deep/這種樣式穿透,版本^和~的區(qū)別在于^只能鎖住第一位數2025-01-01