parent
8ec69cbc3f
commit
04ac6c467a
@ -46,6 +46,26 @@ describe('api: createApp', () => {
|
|||||||
expect(`already been mounted`).toHaveBeenWarned()
|
expect(`already been mounted`).toHaveBeenWarned()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('unmount', () => {
|
||||||
|
const Comp = {
|
||||||
|
props: {
|
||||||
|
count: {
|
||||||
|
default: 0
|
||||||
|
}
|
||||||
|
},
|
||||||
|
setup(props: { count: number }) {
|
||||||
|
return () => props.count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const root = nodeOps.createElement('div')
|
||||||
|
const app = createApp()
|
||||||
|
app.mount(Comp, root)
|
||||||
|
|
||||||
|
app.unmount(root)
|
||||||
|
expect(serializeInner(root)).toBe(``)
|
||||||
|
})
|
||||||
|
|
||||||
test('provide', () => {
|
test('provide', () => {
|
||||||
const app = createApp()
|
const app = createApp()
|
||||||
app.provide('foo', 1)
|
app.provide('foo', 1)
|
||||||
|
@ -24,6 +24,7 @@ export interface App<HostElement = any> {
|
|||||||
rootContainer: HostElement | string,
|
rootContainer: HostElement | string,
|
||||||
rootProps?: Data
|
rootProps?: Data
|
||||||
): ComponentPublicInstance
|
): ComponentPublicInstance
|
||||||
|
unmount(rootContainer: HostElement | string): void
|
||||||
provide<T>(key: InjectionKey<T> | string, value: T): this
|
provide<T>(key: InjectionKey<T> | string, value: T): this
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,6 +198,10 @@ export function createAppAPI<HostNode, HostElement>(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
unmount(rootContainer: HostElement) {
|
||||||
|
render(null, rootContainer)
|
||||||
|
},
|
||||||
|
|
||||||
provide(key, value) {
|
provide(key, value) {
|
||||||
if (__DEV__ && key in context.provides) {
|
if (__DEV__ && key in context.provides) {
|
||||||
warn(
|
warn(
|
||||||
|
@ -29,7 +29,7 @@ export const createApp = (): App<Element> => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const mount = app.mount
|
const { mount, unmount } = app
|
||||||
app.mount = (component, container, props): any => {
|
app.mount = (component, container, props): any => {
|
||||||
if (isString(container)) {
|
if (isString(container)) {
|
||||||
container = document.querySelector(container)!
|
container = document.querySelector(container)!
|
||||||
@ -52,6 +52,18 @@ export const createApp = (): App<Element> => {
|
|||||||
return mount(component, container, props)
|
return mount(component, container, props)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.unmount = container => {
|
||||||
|
if (isString(container)) {
|
||||||
|
container = document.querySelector(container)!
|
||||||
|
if (!container) {
|
||||||
|
__DEV__ &&
|
||||||
|
warn(`Failed to unmount app: mount target selector returned null.`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unmount(container)
|
||||||
|
}
|
||||||
|
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user