]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(runtime-core): support app.unmount(container) (#601)
authorlikui <2218301630@qq.com>
Thu, 16 Jan 2020 17:23:47 +0000 (01:23 +0800)
committerEvan You <yyx990803@gmail.com>
Thu, 16 Jan 2020 17:23:47 +0000 (12:23 -0500)
close #593

packages/runtime-core/__tests__/apiApp.spec.ts
packages/runtime-core/src/apiCreateApp.ts
packages/runtime-dom/src/index.ts

index 31096954e6d1329a038f5307cbc203114874e404..7ade99e9e94d0751377fd060777c6b6e95670528 100644 (file)
@@ -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)
index a07b14ca0d4ed6bc9cddb85465ec834a56b62cdc..fdd377d02644723ea7473727d8fd88d4c2ecfe13 100644 (file)
@@ -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
 }
 
@@ -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(
index 62885cb2fb09a62a6f9a514b55f3d30e0c70d88b..8d5de566483ead73dc1d2266f2067754a9dd61a8 100644 (file)
@@ -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)!
@@ -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
 }