-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type Vue from 'vue' | ||
import { VueConstructor } from 'vue/types/umd' | ||
import { getCurrentVue } from './runtimeContext' | ||
import { warn } from './utils' | ||
|
||
export interface App { | ||
config: VueConstructor['config'] | ||
use: VueConstructor['use'] | ||
mixin: VueConstructor['mixin'] | ||
component: VueConstructor['component'] | ||
directive: VueConstructor['directive'] | ||
mount: Vue['$mount'] | ||
unmount: Vue['$destroy'] | ||
} | ||
|
||
export function createApp(rootComponent: any, rootProps: any = undefined): App { | ||
const V = getCurrentVue()! | ||
|
||
let mountedVM: Vue | undefined = undefined | ||
|
||
return { | ||
config: V.config, | ||
use: V.use.bind(V), | ||
mixin: V.mixin.bind(V), | ||
component: V.component.bind(V), | ||
directive: V.directive.bind(V), | ||
mount: (el, hydrating) => { | ||
if (!mountedVM) { | ||
mountedVM = new V({ propsData: rootProps, ...rootComponent }) | ||
mountedVM.$mount(el, hydrating) | ||
return mountedVM | ||
} else { | ||
if (__DEV__) { | ||
warn( | ||
`App has already been mounted.\n` + | ||
`If you want to remount the same app, move your app creation logic ` + | ||
`into a factory function and create fresh app instances for each ` + | ||
`mount - e.g. \`const createMyApp = () => createApp(App)\`` | ||
) | ||
} | ||
return mountedVM | ||
} | ||
}, | ||
unmount: () => { | ||
if (mountedVM) { | ||
mountedVM.$destroy() | ||
mountedVM = undefined | ||
} else if (__DEV__) { | ||
warn(`Cannot unmount an app that is not mounted.`) | ||
} | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const Vue = require('vue/dist/vue.common.js') | ||
const { createApp, defineComponent, ref, h } = require('../src') | ||
|
||
describe('createApp', () => { | ||
it('should work', async () => { | ||
const vm = new Vue({ | ||
setup() { | ||
return { | ||
a: ref(1), | ||
} | ||
}, | ||
template: '<p>{{a}}</p>', | ||
}).$mount() | ||
|
||
await Vue.nextTick() | ||
expect(vm.a).toBe(1) | ||
expect(vm.$el.textContent).toBe('1') | ||
}) | ||
|
||
it('should work with rootProps', async () => { | ||
const app = createApp( | ||
defineComponent({ | ||
props: { | ||
msg: String, | ||
}, | ||
template: '<p>{{msg}}</p>', | ||
}), | ||
{ | ||
msg: 'foobar', | ||
} | ||
) | ||
const vm = app.mount() | ||
|
||
await Vue.nextTick() | ||
expect(vm.$el.textContent).toBe('foobar') | ||
}) | ||
|
||
it('should work with components', async () => { | ||
const Foo = defineComponent({ | ||
props: { | ||
msg: { | ||
type: String, | ||
required: true, | ||
}, | ||
}, | ||
template: '<p>{{msg}}</p>', | ||
}) | ||
|
||
const app = createApp( | ||
defineComponent({ | ||
props: { | ||
msg: String, | ||
}, | ||
template: '<Foo :msg="msg"/>', | ||
}), | ||
{ | ||
msg: 'foobar', | ||
} | ||
) | ||
app.component('Foo', Foo) | ||
const vm = app.mount() | ||
|
||
await Vue.nextTick() | ||
expect(vm.$el.textContent).toBe('foobar') | ||
}) | ||
}) |