[vue] 暴露组件为一个Vue实例(用于在js调用组件。类似this.$toast

首先写好一个组件

然后将他转为vue实例,并且暴露实例

 

import Vue from 'vue'
import Component from './index.vue'

const Loading = Vue.extend(Component)

const instance = new Loading({
  el: document.createElement('div')
})

instance.show = () => {
  instance.value = true
  document.body.appendChild(instance.$el)
}

instance.hide = () => {
  document.body.removeChild(instance.$el)
}

export default instance

  如果需要全局注册,暴露install方法即可

 

posted @ 2021-01-01 11:53  re-saika  阅读(476)  评论(0)    收藏  举报