基于vue寫(xiě)一個(gè)全局Message組件的實(shí)現(xiàn)
不知道大家在用一些UI框架,比如Element-ui的時(shí)候,有沒(méi)有覺(jué)得一些全局組件。this.$message(),this.Toast()等,用起來(lái)很爽。他們不像其他的組件,需要去導(dǎo)入,去注冊(cè)。麻煩的很。
看了Element的源碼,自己也擼了一個(gè)。其中包括了以前沒(méi)有接觸過(guò)的插件知識(shí),哎,感覺(jué)自己對(duì)Vue的理解還是不夠,只停留在了使用的這階段。需要更多的往深層次的地方去鉆。不說(shuō)廢話(huà)了,直接上代碼。
效果演示
全局組件需要一個(gè)index.js文件去注冊(cè)
BlogMessage.vue
這里的script是用ts寫(xiě)的。大家只需將這里稍做修改就可以了
<template> <transition name="slide"> <div class="message-wrap" :class="type" v-if="visible"> <div class="content">{{content}}</div> </div> </transition> </template> <script lang='ts'> import { Component, Vue, Watch, Prop } from "vue-property-decorator"; @Component({ components: {} }) export default class extends Vue { private content: string = ""; private visible: boolean = false; private type: string = "info"; // 'success','error' private startTimer() { window.setTimeout(() => { this.visible = false; }, 3000); } private mounted() { this.startTimer(); } } </script> <style scoped lang="scss"> .message-wrap { position: fixed; background-color: #44b0f3; color: #ffffff; left: 40%; width: 20%; top: 25px; height: 40px; z-index: 1001; border-radius: 4px; text-align: center; border: 1px solid #ebeef5; .content { line-height: 40px; } } .error { background-color: #fef0f0; color: #f56c6c; } .success { background-color: #f0f9eb; color: #67c23a; } .slide-enter-active, .slide-leave-active { transition: all 0.3s cubic-bezier(1, 0.5, 0.8, 1); transition: all 0.2s ease; } .slide-enter, .slide-leave-to { transform: translateY(-20px); opacity: 0; } </style>
index.js
import Vue from 'vue' import BlogMessage from './BlogMessage.vue' const MessageBox = Vue.extend(BlogMessage) // 創(chuàng)建的是一個(gè)組件構(gòu)造器,不是實(shí)例 const Message = { install: (options, type, duration) => { if (options === undefined || options === null) { options = { content: '' } } else if (typeof options === 'string' || typeof options === 'number') { options = { content: options } if (type != undefined && options != null) { options.type = type; } } let instance = new MessageBox({ data: options }).$mount() document.body.appendChild(instance.$el) // 添加dom元素 Vue.nextTick(() => { // dom元素渲染完成后執(zhí)行回調(diào) instance.visible = true }) } } Vue.prototype.$message = Message.install; ['success', 'error'].forEach(type => { Vue.prototype.$message[type] = (content) => { return Vue.prototype.$message(content, type) } }) export default Message
使用組件
1.全局注冊(cè)
import Vue from 'vue'; import Message from '@/components/common/message'; Vue.use(Message);
2.調(diào)用方法
private test1() { this.$message("這是一條普通消息"); } private test2() { this.$message.success("這是一條成功消息"); // this.$message("這是一條成功消息", "success"); } private test3() { this.$message.error("這是一條失敗消息"); // this.$message("這是一條失敗消息", "error"); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解vue.js移動(dòng)端配置flexible.js及注意事項(xiàng)
最近在用vue做移動(dòng)端項(xiàng)目,網(wǎng)上找了一些移動(dòng)端適配的方案,個(gè)人覺(jué)得手淘團(tuán)隊(duì)flexible.js還是比較容易上手,在這里做下總結(jié)。對(duì)vue.js移動(dòng)端配置flexible.js 相關(guān)知識(shí)感興趣的朋友跟隨小編一起看看吧2019-04-04解決vue2中使用elementUi打包報(bào)錯(cuò)的問(wèn)題
這篇文章主要介紹了解決vue2中使用elementUi打包報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09vue.js集成echarts時(shí)遇到的一些問(wèn)題總結(jié)
這篇文章主要給大家總結(jié)介紹了關(guān)于vue.js集成echarts遇到的一些問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04壓縮Vue.js打包后的體積方法總結(jié)(Vue.js打包后體積過(guò)大問(wèn)題)
大家都知道,Vuejs的 CLI工具 是基于 webpack 來(lái)實(shí)現(xiàn)的,所以在項(xiàng)目打包后,會(huì)生成的文件會(huì)很大。 主要原因是 webpack 將我們所有文件都打包成一個(gè)js文件,即使再小的項(xiàng)目,打包之后文件都會(huì)變得很大。 下面講講最近我遇到的相同問(wèn)題。2020-02-02vue.js 實(shí)現(xiàn)點(diǎn)擊div標(biāo)簽時(shí)改變樣式
這篇文章主要介紹了vue.js 實(shí)現(xiàn)點(diǎn)擊div標(biāo)簽時(shí)改變樣式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08Vue $nextTick 為什么能獲取到最新Dom源碼解析
這篇文章主要為大家介紹了Vue $nextTick 為什么能獲取到最新Dom源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Vue項(xiàng)目?jī)?yōu)化打包之前端必備加分項(xiàng)
相信現(xiàn)在很多人都是用Vue做過(guò)了各種項(xiàng)目,但是項(xiàng)目代碼做完和上線并不代表這結(jié)束,還有上線以后的優(yōu)化也是很重要的一點(diǎn),這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目?jī)?yōu)化打包的相關(guān)資料,需要的朋友可以參考下2021-09-09