1.怎么做
index.ts
const app = createApp()
app.use(插件名称)
import type {App, VNode} from 'vue'
import Loading from './index.vue'
import {createVNode, render} from 'vue'
export default {
install(app: App) {
const Vnode : VNode = createVNode(Loading)
render(Vnode, document.body)
app.config.globalProperties.$loading = {
show: Vnode.component?.exposed?.show,
hide: Vnode.component?.exposed?.hide
}
console.log(app, 1233, Vnode)
}
}
app.use(Loading)
instance?.proxy?.$loading.show()
type Lod = {
show: ()=>void,
hide: ()=>void
}
declare module '@vue/runtime-core'{
export interface ComponentCustomProperties {
$loading: Lod
}
}
2.作用
面试常考题
编写常用插件
3.拓展
import type {App} from 'vue'
import app from './main'
type plugin {
install: (app: App, ...options:any[])=> void
}
const addedList = new Set()
app.MyUse = function MyUse<T>(plugin:T):T{
if (addedList.has(plugin)){
console.error('该组件已添加,请不要重复注册')
return
}
if (typeof plugin === 'function'){
plugin(app, ...options)
} else {
plugin.install(app,...options)
}
addedList.add(plugin)
return app
}