forked from tusen-ai/naive-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.ts
52 lines (47 loc) · 1.12 KB
/
create.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { App } from 'vue'
import version from './version'
type ComponentType = any
export interface NUiInstance {
version: string
componentPrefix: string
install: (app: App) => void
}
interface NUiCreateOptions {
components?: ComponentType[]
componentPrefix?: string
}
function create ({
componentPrefix = 'N',
components = []
}: NUiCreateOptions = {}): NUiInstance {
const installTargets: App[] = []
function registerComponent (
app: App,
name: string,
component: ComponentType
): void {
const registered = app.component(componentPrefix + name)
if (!registered) {
app.component(componentPrefix + name, component)
}
}
function install (app: App): void {
if (installTargets.includes(app)) return
installTargets.push(app)
components.forEach((component) => {
const { name, alias } = component
registerComponent(app, name, component)
if (alias) {
alias.forEach((aliasName: string) => {
registerComponent(app, aliasName, component)
})
}
})
}
return {
version,
componentPrefix,
install
}
}
export default create