Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime-core): unmount app (#593) #601

Merged
merged 2 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/runtime-core/__tests__/apiApp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ describe('api: createApp', () => {
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', () => {
const app = createApp()
app.provide('foo', 1)
Expand Down
5 changes: 5 additions & 0 deletions packages/runtime-core/src/apiCreateApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface App<HostElement = any> {
rootContainer: HostElement | string,
rootProps?: Data
): ComponentPublicInstance
unmount(rootContainer: HostElement | string): void
provide<T>(key: InjectionKey<T> | string, value: T): this
}

Expand Down Expand Up @@ -197,6 +198,10 @@ export function createAppAPI<HostNode, HostElement>(
}
},

unmount(rootContainer: HostElement) {
render(null, rootContainer)
},

provide(key, value) {
if (__DEV__ && key in context.provides) {
warn(
Expand Down
14 changes: 13 additions & 1 deletion packages/runtime-dom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const createApp = (): App<Element> => {
})
}

const mount = app.mount
const { mount, unmount } = app
app.mount = (component, container, props): any => {
if (isString(container)) {
container = document.querySelector(container)!
Expand All @@ -52,6 +52,18 @@ export const createApp = (): App<Element> => {
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
}

Expand Down