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

vue使用Vue.extend創(chuàng)建全局toast組件實例

 更新時間:2023年03月04日 14:18:19   作者:吳冬雪~  
這篇文章主要介紹了vue使用Vue.extend創(chuàng)建全局toast組件實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Vue.extend創(chuàng)建全局toast組件

src -> components -> Toast -> toast.vue

<template>
? <transition name="fades">
? ? <div class="Errormes" ?v-if="show">{{txt}}</div>
? </transition>
</template>
?
<script>
export default {
? name: 'Toast',
? data () {
? ? return {
? ? ? txt: '',
? ? ? show: false
? ? }
? }
}
</script>
<style lang="less" scoped>
.fades-enter-active, .fades-leave-active {
? transition: opacity 0.5s;
}
.fades-enter, .fades-leave-active {
? opacity: 0;
}
/* 提示彈框 */
.Errormes {
? position: fixed;
? top: 40%;
? left: 50%;
? -webkit-transform: translate(-50%, -50%);
? transform: translate(-50%, -50%);
? padding: 20px 30px;
? background: rgba(0, 0, 0, 0.8);
? border-radius: 16px;
? color: #fff;
? font-size: 28px;
? z-index: 999999;
? max-width: 80%;
? height: auto;
? line-height: 60px;
? text-align: center;
}
</style>

src -> components -> Toast -> index.js

import Toast from './toast.vue'
?
const toast = {}
toast.install = (vue) => {
? const ToastClass = vue.extend(Toast)
? const instance = new ToastClass()
? instance.$mount(document.createElement('div'))
? document.body.appendChild(instance.$el)
?
? let t = null
? const ToastMain = {
? ? show (msg, seconds = 2000) {
? ? ? if (t) clearTimeout(t)
? ? ? instance.txt = msg
? ? ? instance.show = true
? ? ? t = setTimeout(() => {
? ? ? ? instance.show = false
? ? ? }, seconds)
? ? }
? }
? vue.prototype.$toast = ToastMain
}
?
export default toast

main.js

import Vue from 'vue'
import App from './App.vue'
import toast from '@/components/Toast/index'
?
Vue.use(toast)

使用

$toast.show(message)

關于vue.extend的理解應用

基本概念

Vue.extend( options )

使用基礎 Vue 構造器,創(chuàng)建一個“子類”。參數(shù)是一個包含組件選項的對象。

一般,我們會用 Vue.extend 接收一個組件對象來創(chuàng)建一個構造器,再利用創(chuàng)建的構造器 new 一個實例,并將這個實例掛載到一個元素上。

官網(wǎng)基本示例

<div id="mount-point"></div>
// 創(chuàng)建構造器
var Profile = Vue.extend({
? template: '<p>{{firstName}} {{lastName}} aka {{alias}}</p>',
? data: function () {
? ? return {
? ? ? firstName: 'Walter',
? ? ? lastName: 'White',
? ? ? alias: 'Heisenberg'
? ? }
? }
})
// 創(chuàng)建 Profile 實例,并掛載到一個元素上。
new Profile().$mount('#mount-point')

data 選項是特例,需要注意 - 在 Vue.extend() 中它必須是函數(shù)

結果如下:

<p>Walter White aka Heisenberg</p>

應用

Vue.extend 屬于 Vue 的全局 API,在實際業(yè)務開發(fā)中我們很少使用,因為相比常用的 Vue.component 寫法使用 extend 步驟要更加繁瑣一些。

但是在一些獨立組件開發(fā)場景中,例如要實現(xiàn)一個類似于 window.alert() 提示組件,要求像調(diào)用 JS 函數(shù)一樣調(diào)用它,這時候 Vue.extend + $mount 這對組合就是我們需要去關注的。

1、vue $mount 和 el的區(qū)別說明

在應用之前我們先了解一下2個東西 —— $mount 和 el,兩者在使用效果上沒有任何區(qū)別,都是為了將實例化后的vue掛載到指定的dom元素中。

如果在實例化vue的時候指定el,則該vue將會渲染在此el對應的dom中,反之,若沒有指定el,則vue實例會處于一種“未掛載”的狀態(tài),此時可以通過$mount來手動執(zhí)行掛載。

注:如果$mount沒有提供參數(shù),模板將被渲染為文檔之外的的元素,并且你必須使用原生DOM API把它插入文檔中。

var MyComponent = Vue.extend({
?template: '<div>Hello!</div>'
})
?
// 創(chuàng)建并掛載到 #app (會替換 #app)
new MyComponent().$mount('#app')
?
// 同上
new MyComponent({ el: '#app' })
?
// 或者,在文檔之外渲染并且隨后掛載
var component = new MyComponent().$mount()
document.getElementById('app').appendChild(component.$el)

2、Vue.extend實現(xiàn)Loading插件($mount)

<div id="root">
? ? <button @click="showLoading">顯示Loading</button>
</div>
function Loading(msg) {
? ? ? ? // 創(chuàng)建構造器
? ? ? ? const Loading = Vue.extend({
? ? ? ? ? template: '<div id="loading-msg">{{ msg }}</div>',
? ? ? ? ? props: {
? ? ? ? ? ? msg: {
? ? ? ? ? ? ? type: String,
? ? ? ? ? ? ? default: '加載中'
? ? ? ? ? ? }
? ? ? ? ? },
? ? ? ? ? name: 'Loading'
? ? ? ? })
?
? ? ? ? // 創(chuàng)建掛載div
? ? ? ? const div = document.createElement('div')
? ? ? ? div.setAttribute('id', 'loading-div')
? ? ? ? document.body.append(div)
?
? ? ? ? // 創(chuàng)建實例并掛載到一個元素上
? ? ? ? new Loading().$mount('#loading-div')
?
? ? ? ? // 返回一個移除元素的function
? ? ? ? return () => {
? ? ? ? ? document.body.removeChild(document.getElementById('loading-div'))
? ? ? ? }
}
?
// 掛載到vue實例上
Vue.prototype.$loading = Loading
?
?new Vue({
? ? ?el: '#root',
? ? ?methods: {
? ? ? ? showLoading() {
? ? ? ? ? ? const hide = this.$loading('正在加載,請稍等...')
? ? ? ? ? ? setTimeout(() => {
? ? ? ? ? ? ? hide()
? ? ? ? ? ? }, 1500)
? ? ? ? }
? ? ?}
})

3、Vue.extend實現(xiàn)信息彈窗插件(el)

新建一個popBox.vue

<template>
? <div id="confirm" v-if='flag'>
? ? <div class="contents" >
? ? ? <div class="content-top">{{ text.title }}</div>
? ? ? <div class="content-center">{{ text.msg }}</div>
? ? ? <div class="content-bottom">
? ? ? ? <button @click='show' class="left">{{ text.btn.ok }}</button>
? ? ? ? <button @click='hide' class="right">{{ text.btn.no }}</button>
? ? ? </div>
? ? </div>
? </div>
</template>
?
<script>
?
export default {
? data () {
? ? return {
? ? ? flag: true,
? ? ? text: {
? ? ? ? ? title:'標題',
? ? ? ? ? msg: '這是一個信息彈出框組件',
? ? ? ? ? btn: {
? ? ? ? ? ? ? ok: '確定',
? ? ? ? ? ? ? no: '取消'
? ? ? ? ? }
? ? ? }
? ? }
? },
?
? methods: {
? ? show (){
? ? ? this.flag = false;
? ? },
?
? ? hide() {
? ? ? this.flag = false;
? ? }
? }
}
?
</script>

新建一個popBox.js

import Vue from 'vue'
import PopBox from './popBox.vue'
?
// Vue.extend返回一個實例創(chuàng)建的構造器,但實例構造器需要進行掛載到頁面中
let popBox = Vue.extend(PopBox) ??
?
popBox.install = (vue, text) => {
? ? ??
? ? ? ? // 在body中動態(tài)創(chuàng)建一個div元素,之后此div將會替換成整個vue文件的內(nèi)容
? ? ? ? // 此時的popBoxDom通俗講就是相當于是整個組件對象,通過對象調(diào)用屬性的方法來進行組件中數(shù)據(jù)的使用
? ? ? ? let popBoxDom = new popBox({
? ? ? ? ? ? el: document.createElement('div')
? ? ? ? })
?
? ? ? ??
? ? ? ? // 可以通過$el屬性來訪問創(chuàng)建的組件實例
? ? ? ? document.body.appendChild(popBoxDom.$el)
?
? ?
? ? ? ? // 將需要傳入的文本內(nèi)容傳給組件實例
? ? ? ? popBoxDom.text = text;
?
? ? ? ? // 返回一個promise,進行異步操作,成功時返回,失敗時返回
? ? ? ? return new Promise((res, rej) => { ? ??
? ? ? ? ? ? popBoxDom.show = () => { ??
? ? ? ? ? ? ? ? res() ? //正確時返回的操作
? ? ? ? ? ? ? ? popBoxDom.flag = false;
? ? ? ? ? ? }
?
? ? ? ? ? ? popBoxDom.hide = ()=>{
? ? ? ? ? ? ? ? rej() ? //失敗時返回的操作
? ? ? ? ? ? ? ? popBoxDom.flag = false;
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? vue.prototype.$popBox = popBox ? ?
? ? })
}
?
// 將邏輯函數(shù)進行導出和暴露
export default popBox

頁面使用

import PopBox from './popBox.js'
?
Vue.use(popBOx);
?
?
this.$popBox({
? ? ? title:'標題',
? ? ? msg:'內(nèi)容',
? ? ? btn:{ ok:'確定', no:'取消'}
}).then(()=>{
? ? ? console.log('ok')
}).catch(()=>{
? ? ? console.log('no')
})

其他

import toastCom from "./Toast";
?
const toast = {};
toast.install = vue => {
?const ToastCon = vue.extend(toastCom);
?const ins = new ToastCon();
?ins.$mount(document.createElement("div"));
?document.body.appendChild(ins.$el);
?console.log(ins.toast)
?vue.prototype.$toast = ins.toast;
};
?
?
?
const globalComponent = {
?install: function(Vue) {
? ?Vue.use(toast);
?}
};
?
export default globalComponent;

總結

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 解讀Vue3中keep-alive和動態(tài)組件的實現(xiàn)邏輯

    解讀Vue3中keep-alive和動態(tài)組件的實現(xiàn)邏輯

    這篇文章主要介紹了Vue3中keep-alive和動態(tài)組件的實現(xiàn)邏輯,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • element-plus 如何設置 el-date-picker 彈出框位置

    element-plus 如何設置 el-date-picker 彈出框位置

    el-date-picker 組件會自動根據(jù)空間范圍進行選擇比較好的彈出位置,但特定情況下,它自動計算出的彈出位置并不符合我們的實際需求,故需要我們手動設置,這篇文章主要介紹了element-plus 如何設置 el-date-picker 彈出框位置,需要的朋友可以參考下
    2024-07-07
  • Vue實現(xiàn)簡單基礎的圖片裁剪功能

    Vue實現(xiàn)簡單基礎的圖片裁剪功能

    這篇文章主要為大家詳細介紹了如何利用Vue2實現(xiàn)簡單基礎的圖片裁剪功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-09-09
  • Element 的 el-table 表格實現(xiàn)單元格合并功能

    Element 的 el-table 表格實現(xiàn)單元格合并功能

    這篇文章主要介紹了Element 的 el-table 表格實現(xiàn)單元格合并功能,本文通過示例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧
    2024-07-07
  • vue 根據(jù)數(shù)組中某一項的值進行排序的方法

    vue 根據(jù)數(shù)組中某一項的值進行排序的方法

    這篇文章主要介紹了vue 根據(jù)數(shù)組中某一項的值進行排序的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • vue3 setup語法糖之父子組件之間的傳值方法

    vue3 setup語法糖之父子組件之間的傳值方法

    父組件向子組件傳值的時候,子組件是通過props來接收的,然后以變量的形式將props傳遞到setup語法糖果中使用,本文給大家介紹vue3 setup語法糖之父子組件之間的傳值,感興趣的朋友一起看看吧
    2023-12-12
  • vue使用Proxy實現(xiàn)雙向綁定的方法示例

    vue使用Proxy實現(xiàn)雙向綁定的方法示例

    這篇文章主要介紹了vue使用Proxy實現(xiàn)雙向綁定的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • 淺談vuejs實現(xiàn)數(shù)據(jù)驅(qū)動視圖原理

    淺談vuejs實現(xiàn)數(shù)據(jù)驅(qū)動視圖原理

    這篇文章主要介紹了淺談vuejs實現(xiàn)數(shù)據(jù)驅(qū)動視圖原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • 從零開始在vue-cli4配置自適應vw布局的實現(xiàn)

    從零開始在vue-cli4配置自適應vw布局的實現(xiàn)

    這篇文章主要介紹了從零開始在vue-cli4配置自適應vw布局,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • vue?element-plus中el-input修改邊框border的方法

    vue?element-plus中el-input修改邊框border的方法

    element樣式還是蠻好的,只是有時候我們需要做一些調(diào)整,比如el-input的邊框,下面這篇文章主要給大家介紹了關于vue?element-plus中el-input修改邊框border的相關資料,需要的朋友可以參考下
    2022-09-09

最新評論