Vue組件掛載到全局方法中

組件:src/components/loading/loading.vue

<template>

? ? //...

</template>

<script>

export default {

? ? data() {

? ? ? ? visible:false

????}

}

</script>

<style>

//...

</style>

src/components/loading/index.js

import Vue from 'vue'

import loading from './loading.vue'

const MyLoading = Vue.extend(loading)

const install = () => {

? ? Vue.prototype.$loading = () => {

? ? ? ? var instance = new MyLoading({

? ? ? ? ? ? data:{

? ? ? ? ? ? ? ? visible:true

????????????}

????????})

? ? ? ? instance.id = 'loading-component'

? ? ? ? instance.vm = instance.$mount()

? ? ? ? instance.vm.close = () => {

? ? ? ? ? ? instance.visible = false

????????}

? ? ? ? document.body.appendChild(instance.vm.$el)

? ? ? ? return instance.vm

????}

}

export default {

? ? install

}

然后在main.js中引入

import Loading from './components/loading/index.js'

Vue.use(Loading)

然后在每個(gè)組件中都可以通過方法調(diào)用了。(示例3秒后關(guān)閉loading)

var loading = this.$loading()

setTimeout(()=>{

? ? loading.close();//

},3000)

歡迎指正。。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容