[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方法即可
前端远比想象中的还要复杂,需要学习的还有很多,脚踏实地记录好遇到的麻烦与经验,三省吾身,才能慢慢积累经验。

浙公网安备 33010602011771号