app.use重写

MyUse.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import type { App } from 'vue'

import { app } from '../main'

interface Use {
install: (app: App, ...options: any[]) => void
}

const installList = new Set()

export function MyUse<T extends Use>(plugin: T, ...options: any[]) {
if (installList.has(plugin)) {
console.log('has been registered');
} else {
plugin.install(app, ...options)
installList.add(plugin)
}
}

main.ts

1
2
3
4
5
export const app = createApp(App)

import { MyUse } from './hooks/MyUse'

MyUse(Loading)