(rootComponent: any, rootProps: any = undefined)
| 19 | } |
| 20 | |
| 21 | export function createApp(rootComponent: any, rootProps: any = undefined): App { |
| 22 | const V = getVueConstructor()! |
| 23 | |
| 24 | let mountedVM: Vue | undefined = undefined |
| 25 | |
| 26 | let provide: Record<any, any> = {} |
| 27 | |
| 28 | const app: App = { |
| 29 | config: V.config, |
| 30 | use: V.use.bind(V), |
| 31 | mixin: V.mixin.bind(V), |
| 32 | component: V.component.bind(V), |
| 33 | provide<T>(key: InjectionKey<T> | symbol | string, value: T) { |
| 34 | provide[key as any] = value |
| 35 | return this |
| 36 | }, |
| 37 | directive(name: string, dir?: Directive | undefined): any { |
| 38 | if (dir) { |
| 39 | V.directive(name, dir as any) |
| 40 | return app |
| 41 | } else { |
| 42 | return V.directive(name) |
| 43 | } |
| 44 | }, |
| 45 | mount: (el, hydrating) => { |
| 46 | if (!mountedVM) { |
| 47 | mountedVM = new V({ |
| 48 | propsData: rootProps, |
| 49 | ...rootComponent, |
| 50 | provide: { ...provide, ...rootComponent.provide }, |
| 51 | }) |
| 52 | mountedVM.$mount(el, hydrating) |
| 53 | return mountedVM |
| 54 | } else { |
| 55 | if (__DEV__) { |
| 56 | warn( |
| 57 | `App has already been mounted.\n` + |
| 58 | `If you want to remount the same app, move your app creation logic ` + |
| 59 | `into a factory function and create fresh app instances for each ` + |
| 60 | `mount - e.g. \`const createMyApp = () => createApp(App)\`` |
| 61 | ) |
| 62 | } |
| 63 | return mountedVM |
| 64 | } |
| 65 | }, |
| 66 | unmount: () => { |
| 67 | if (mountedVM) { |
| 68 | mountedVM.$destroy() |
| 69 | mountedVM = undefined |
| 70 | } else if (__DEV__) { |
| 71 | warn(`Cannot unmount an app that is not mounted.`) |
| 72 | } |
| 73 | }, |
| 74 | } |
| 75 | return app |
| 76 | } |
searching dependent graphs…