最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue開(kāi)發(fā)自定義的全局公共組件詳解

 更新時(shí)間:2024年10月11日 10:41:13   作者:夢(mèng)一場(chǎng)江南煙雨  
本文介紹了如何開(kāi)發(fā)自定義全局公共組件的兩種方法,第一種方法是通過(guò)在components文件夾中創(chuàng)建新的組件文件夾,例如Loading文件夾,并在其中創(chuàng)建index.js和index.vue文件,通過(guò)在vue的入口文件main.js中進(jìn)行引入,可以實(shí)現(xiàn)組件的全局調(diào)用

vue開(kāi)發(fā)自定義的全局公共組件

這里我主要介紹兩種自定義全局公共組件的方法

第一種

首先在components中新建一個(gè)文件夾,我這里做的是全局加載動(dòng)畫(huà)組件所以命名的是Loading文件夾

如圖:

其中index.js為組件加載文件,index.vue為組件模板文件

index.js文件:

// 引入組件
import Loading from './index.vue'
// 創(chuàng)建個(gè)空對(duì)象
const obj = {}
// 設(shè)置安裝方法
obj.install = (Vue) => {
    // 1. 創(chuàng)建組件構(gòu)造器
    const LoadingConstructor = Vue.extend(Loading)
    // 2. new 的方式,根據(jù)組件構(gòu)造器,可以創(chuàng)建出來(lái)一個(gè)組件對(duì)象
    const loading = new LoadingConstructor()
    // 3. 將組件對(duì)象手動(dòng)掛載到某一個(gè)元素上掛載會(huì)替換元素內(nèi)容,這里創(chuàng)建了一個(gè)div元素來(lái)作為被替換內(nèi)容
    loading.$mount(document.createElement('div'))
    // 4. 將組件添加到dom中
    document.body.appendChild(loading.$el)
    // 5. 將$loading掛載到Vue的原型上
    Vue.prototype.$loading = loading
}
// 導(dǎo)出對(duì)象
export default obj

index.vue文件:

<template>
    <div class="loading-wrap" v-if="block || bar || water">
        <!-- 條狀加載動(dòng)畫(huà) -->
        <div class="bar-load" v-if="bar">
            <span class="item"></span>
            <span class="item"></span>
            <span class="item"></span>
            <span class="item"></span>
            <span class="item"></span>
        </div>
        <!-- 方塊狀加載動(dòng)畫(huà) -->
        <div class="block-load" v-if="block">
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
        </div>
		<!-- 水波加載動(dòng)畫(huà) -->
		<div class="water-load" v-if="water">
			<span class="circle circle1"></span>
			<span class="circle circle2"></span>
		</div>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                block: false,
                bar: false,
                water: false
            }
        },
        methods: {
            // loading展示
            show(val) {
                // 如果沒(méi)有傳入類型,默認(rèn)為條狀loading
                if (!val) {
                    this.bar = true
                    return
                }
                // 如果傳入的是對(duì)象{類型,加載時(shí)間}
                const { type, duration } = val || {}
                if (typeof val === 'object') {
                    this[type] = true
                    // 如果duration > 0,否則永久展示loading動(dòng)畫(huà)
                    if (duration && duration > 0) {
                        setTimeout(() => {
                            this[type] = false
                        }, duration)
                    }
                    return
                }
                // 如果傳入的就是某個(gè)loading類型
                this[val] = true
            },
            // loading隱藏
            hide() {
                this.block = false
                this.bar = false
            }
        }
    }
</script>
<style lang="scss">
.loading-wrap{
    position: fixed;
    top: 0;
    left: 0;
    z-index: 999;
    height: 100%;
    width: 100%;
    background: rgba(0,0,0,0.35);
    display: flex;
    align-items: center;
    justify-content: center;
}
</style>

<!-- 條狀loading -->
<style lang="scss">
.loading-wrap{
    .bar-load{
        width: 100px;
        height: 100px;
        text-align: center;
        line-height: 100px;
        position: relative;
        .item{
            display: inline-block;
            opacity: 0;
            width: 6px;
            height: 30px;
            margin: 0 5px;
            border-radius: 4px;
            position: relative;
            animation: move 1s ease infinite;
            background: linear-gradient(to bottom, rgb(160, 192, 150), #1989fa, #ffd01e);
            &:nth-child(2){
                animation-delay: .2s;
            }
            &:nth-child(3){
                animation-delay: .4s;
            }
            &:nth-child(4){
                animation-delay: .6s;
            }
            &:nth-child(5){
                animation-delay: .8s;
            }
        }
    }
    @keyframes move {
        0% {
            height: 20px;
            opacity: 0;
            background: linear-gradient(to bottom, rgb(160, 192, 150), #1989fa, #ffd01e);
        }
        50% {
            height: 50px;
            margin: -15px 5px;
            opacity: 1;
            background: linear-gradient(to bottom, rgb(160, 192, 150), #ffd01e, #1989fa);
        }
        100% {
            height: 20px;
            opacity: 0;
            background: linear-gradient(to bottom, rgb(160, 192, 150), #1989fa, #ffd01e);
        }
    }
}
</style>

<!-- 方塊轉(zhuǎn)loading -->
<style lang="scss">
.loading-wrap{
    .block-load{
        width: 150px;
        height: 15px;
        margin: 0 auto;
        text-align: center;
        span{
            display: inline-block;
            opacity: 0;
            width: 15px;
            height: 100%;
            margin-right: 5px;
            background: #1989fa;
            -webkit-transform-origin: right bottom;
            transform-origin: right bottom;
            -webkit-animation: load 1s ease infinite;
            animation: load 1s ease infinite;
            &:last-child{
                margin-right: 0px;
            }
            &:nth-child(1){
                -webkit-animation-delay: 0.13s;
                animation-delay: 0.13s;
            }
            &:nth-child(2){
                -webkit-animation-delay: 0.26s;
                animation-delay: 0.26s;
            }
            &:nth-child(3){
                -webkit-animation-delay: 0.39s;
                animation-delay: 0.39s;
            }
            &:nth-child(4){
                -webkit-animation-delay: 0.52s;
                animation-delay: 0.52s;
            }
            &:nth-child(5){
                -webkit-animation-delay: 0.65s;
                animation-delay: 0.65s;
            }
        }
    }
    @-webkit-keyframes load{
        0%{
            opacity: 1;
            -webkit-transform: scale(1);
        }
        100%{
            opacity: 0;
            -webkit-transform: rotate(90deg) scale(.3);
        }
    }
    @keyframes load{
        0%{
            opacity: 1;
            -webkit-transform: scale(1);
        }
        100%{
            opacity: 0;
            -webkit-transform: rotate(90deg) scale(.3);
        }
    }
}
</style>

<!-- 水波加載loading -->
<style lang="scss" scoped>
.loading-wrap{
	.water-load{
	    width: 100px;
        height: 100px;
        margin: 0 auto;
        text-align: center;
		background: rgb(41, 134, 196);
    	border-radius: 50%;
		position: relative;
		display: flex;
		align-items: center;
		justify-content: center;
		.circle{
			display: inline-block;
            position: absolute;
            width: 90px;
            height: 90px;
            border-radius: 50%;
            // border: 5px solid rgb(246, 247, 248);
            box-shadow: 0 0 0 3px rgb(41, 134, 196);
			overflow: hidden;
		}
		.circle1{
			&::before{
				content: '';
				position: absolute;
				top: 0;
				left: 50%;
				width: 150%;
				height: 150%;
				border-radius: 40%;
				background: rgb(240, 228, 228);
				animation: moveingOne 5s linear infinite;
			}
			&::after{
				content: '';
				position: absolute;
				top: 0;
				left: 50%;
				width: 150%;
				height: 150%;
				border-radius: 45%;
				background: rgba(240, 228, 228, 0.2);
				animation: moveingTwo 8s linear infinite;
			}
		}
		.circle2{
			&::before{
				content: '';
				position: absolute;
				top: 0;
				left: 50%;
				width: 150%;
				height: 150%;
				border-radius: 42%;
				background: rgb(240, 228, 228);
				animation: moveingThree 11s linear infinite;
			}
			&::after{
				content: '';
				position: absolute;
				top: 0;
				left: 50%;
				width: 150%;
				height: 150%;
				border-radius: 40%;
				background: rgba(240, 228, 228, 0.2);
				animation: moveingFour 14s linear infinite;
			}
		}
		@keyframes moveingOne{
            0%{
                transform: translate(-55%,-65%) rotate(0deg);
            }
            100%{
                transform: translate(-55%,-65%) rotate(360deg);
            }
		}
		@keyframes moveingTwo{
            0%{
                transform: translate(-50%,-60%) rotate(0deg);
            }
            100%{
                transform: translate(-50%,-60%) rotate(360deg);
            }
		}
		@keyframes moveingThree{
            0%{
                transform: translate(-50%,-65%) rotate(0deg);
            }
            100%{
                transform: translate(-50%,-65%) rotate(360deg);
            }
		}
		@keyframes moveingFour{
            0%{
                transform: translate(-45%,-60%) rotate(0deg);
            }
            100%{
                transform: translate(-45%,-60%) rotate(360deg);
            }
		}
	}
}
</style>

這是我自己開(kāi)發(fā)的加載動(dòng)畫(huà),各位可直接復(fù)制使用

組件模板開(kāi)發(fā)好后,接下來(lái)就是在vue的入口文件main.js中進(jìn)行引入

main.js文件里加入以下代碼:

import Vue from 'vue'

// 導(dǎo)入組件
import loading from '@/components/Loading'

// 使用
Vue.use(loading)

接下來(lái)等項(xiàng)目跑起來(lái)后我們就可以根據(jù)在組件加載文件index.js里面設(shè)置的調(diào)用方法進(jìn)行全局調(diào)用了

我設(shè)置的是:

    // 將$loading掛載到Vue的原型上
    Vue.prototype.$loading = loading

再看index.vue文件里methods設(shè)置的方法,

因此全局調(diào)用的方法就是:

// 顯示
this.$loading.show()
// show({ obj }) 可接受傳參
// this.$loading.show(arguments) // 顯示
// arguments為參數(shù)可傳:對(duì)象、字符串、或者不傳
// 對(duì)象:{
//     type: 'bar' || 'block' || 'water', // loading形狀:(bar: 條狀,block:方塊狀, water: 水波狀),
//    duration: 3000 // loading展示時(shí)間,不傳或者傳0就一直展示
// }
// 例如:this.$loading.show({
//      type: 'bar',
//     duration: 3000
// })
// 字符串:this.$loading.show('bar')
// 或者不傳:this.$loading.show() // 不傳默認(rèn)loading展示類型為bar
// this.$loading.hide() // 隱藏
// 隱藏
this.$loading.hide()

第一種全局公共組件就這么開(kāi)發(fā)好了,接下來(lái)是另外一種

第二種此處不做介紹

就跟普通的父子組件開(kāi)發(fā)模式類似,不同的是,需要在main.js里面進(jìn)行引入和自定義注冊(cè)組件,全局自定義組件使用

Vue.component("Loading", Loading)

總結(jié)

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

相關(guān)文章

  • vue 清空input標(biāo)簽 中file的值操作

    vue 清空input標(biāo)簽 中file的值操作

    這篇文章主要介紹了vue 清空input標(biāo)簽 中file的值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • Vue使用自定義指令實(shí)現(xiàn)拖拽行為實(shí)例分析

    Vue使用自定義指令實(shí)現(xiàn)拖拽行為實(shí)例分析

    這篇文章主要介紹了Vue使用自定義指令實(shí)現(xiàn)拖拽行為,結(jié)合實(shí)例形式分析了Vue使用自定義指令實(shí)現(xiàn)拖拽行為具體步驟、原理與操作注意事項(xiàng),需要的朋友可以參考下
    2020-06-06
  • Vue3中watchEffect的用途淺析

    Vue3中watchEffect的用途淺析

    這篇文章主要給大家介紹了關(guān)于Vue3中watchEffect用途的相關(guān)資料, watch和watchEffect都是監(jiān)聽(tīng)器,但在寫(xiě)法和使用上有所區(qū)別,本文進(jìn)行了詳細(xì)的介紹,需要的朋友可以參考下
    2021-07-07
  • vue2.0s中eventBus實(shí)現(xiàn)兄弟組件通信的示例代碼

    vue2.0s中eventBus實(shí)現(xiàn)兄弟組件通信的示例代碼

    這篇文章主要介紹了vue2.0s中eventBus實(shí)現(xiàn)兄弟組件通信的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • vue視頻播放暫停代碼

    vue視頻播放暫停代碼

    今天小編就為大家分享一篇vue視頻播放暫停代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • vue3+ts使用bus事件總線的示例代碼

    vue3+ts使用bus事件總線的示例代碼

    這篇文章主要介紹了vue3+ts使用bus事件總線,文中給大家提到了vue總線機(jī)制(bus)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • vue element編輯框根據(jù)值動(dòng)態(tài)確認(rèn)是否是必填項(xiàng)required true or false問(wèn)題

    vue element編輯框根據(jù)值動(dòng)態(tài)確認(rèn)是否是必填項(xiàng)required true or&n

    該文章主要介紹了在編輯數(shù)據(jù)時(shí),根據(jù)是否有時(shí)間數(shù)據(jù)來(lái)決定某項(xiàng)是否為必填項(xiàng),作者提供了一個(gè)簡(jiǎn)單的解決方法,并分享了HTML表單代碼和校驗(yàn)規(guī)則,最后表示希望對(duì)讀者有所幫助
    2026-04-04
  • 使用konva和vue-konva庫(kù)實(shí)現(xiàn)拖拽滑塊驗(yàn)證功能

    使用konva和vue-konva庫(kù)實(shí)現(xiàn)拖拽滑塊驗(yàn)證功能

    這篇文章主要介紹了使用konva和vue-konva完成前端拖拽滑塊驗(yàn)證功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • vue文件使用iconfont解析

    vue文件使用iconfont解析

    這篇文章主要介紹了vue文件使用iconfont解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 關(guān)于iview按需引用后使用this.$Modal報(bào)錯(cuò)的解決

    關(guān)于iview按需引用后使用this.$Modal報(bào)錯(cuò)的解決

    這篇文章主要介紹了關(guān)于iview按需引用后使用this.$Modal報(bào)錯(cuò)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09

最新評(píng)論

思南县| 澜沧| 西乌珠穆沁旗| 东平县| 石景山区| 筠连县| 上栗县| 漳平市| 察雅县| 峨山| 舞阳县| 松阳县| 洛南县| 乐陵市| 治多县| 临江市| 察隅县| 明水县| 奉节县| 南澳县| 惠州市| 云安县| 南靖县| 延吉市| 方山县| 石林| 利川市| 芦溪县| 建湖县| 会同县| 土默特左旗| 囊谦县| 百色市| 公主岭市| 泾川县| 特克斯县| 沙湾县| 繁峙县| 广昌县| 尼勒克县| 吴江市|