refactor: adjust createApp related API signatures

BREAKING CHANGE: `createApp` API has been adjusted.

  - `createApp()` now accepts the root component, and optionally a props
  object to pass to the root component.
  - `app.mount()` now accepts a single argument (the root container)
  - `app.unmount()` no longer requires arguments.

  New behavior looks like the following:

  ``` js
  const app = createApp(RootComponent)
  app.mount('#app')
  app.unmount()
  ```
This commit is contained in:
Evan You
2020-01-23 15:05:38 -05:00
parent eacd390992
commit c07751fd36
25 changed files with 276 additions and 326 deletions

View File

@@ -1,6 +1,6 @@
import {
createApp,
h,
render,
nextTick,
defineComponent,
vModelDynamic,
@@ -20,10 +20,9 @@ const setValue = function(this: any, value: any) {
this.value = value
}
let app: any, root: any
let root: any
beforeEach(() => {
app = createApp()
root = document.createElement('div') as any
})
@@ -44,9 +43,9 @@ describe('vModel', () => {
]
}
})
app.mount(component, root)
render(h(component), root)
const input = root.querySelector('input')
const input = root.querySelector('input')!
const data = root._vnode.component.data
input.value = 'foo'
@@ -75,7 +74,7 @@ describe('vModel', () => {
]
}
})
app.mount(component, root)
render(h(component), root)
const input = root.querySelector('textarea')
const data = root._vnode.component.data
@@ -136,7 +135,7 @@ describe('vModel', () => {
]
}
})
app.mount(component, root)
render(h(component), root)
const number = root.querySelector('.number')
const trim = root.querySelector('.trim')
@@ -176,7 +175,7 @@ describe('vModel', () => {
]
}
})
app.mount(component, root)
render(h(component), root)
const input = root.querySelector('input')
const data = root._vnode.component.data
@@ -219,7 +218,7 @@ describe('vModel', () => {
]
}
})
app.mount(component, root)
render(h(component), root)
const input = root.querySelector('input')
const data = root._vnode.component.data
@@ -262,7 +261,7 @@ describe('vModel', () => {
]
}
})
app.mount(component, root)
render(h(component), root)
const input = root.querySelector('input')
const data = root._vnode.component.data
@@ -314,7 +313,7 @@ describe('vModel', () => {
]
}
})
app.mount(component, root)
render(h(component), root)
const foo = root.querySelector('.foo')
const bar = root.querySelector('.bar')
@@ -384,7 +383,7 @@ describe('vModel', () => {
]
}
})
app.mount(component, root)
render(h(component), root)
const foo = root.querySelector('.foo')
const bar = root.querySelector('.bar')
@@ -437,7 +436,7 @@ describe('vModel', () => {
]
}
})
app.mount(component, root)
render(h(component), root)
const input = root.querySelector('select')
const foo = root.querySelector('option[value=foo]')
@@ -494,7 +493,7 @@ describe('vModel', () => {
]
}
})
app.mount(component, root)
render(h(component), root)
const input = root.querySelector('select')
const foo = root.querySelector('option[value=foo]')

View File

@@ -5,16 +5,15 @@ import {
nextTick,
VNode
} from '@vue/runtime-core'
import { createApp, vShow } from '@vue/runtime-dom'
import { render, vShow } from '@vue/runtime-dom'
const withVShow = (node: VNode, exp: any) =>
withDirectives(node, [[vShow, exp]])
let app: any, root: any
let root: any
beforeEach(() => {
app = createApp()
root = document.createElement('div') as any
root = document.createElement('div')
})
describe('runtime-dom: v-show directive', () => {
@@ -27,7 +26,7 @@ describe('runtime-dom: v-show directive', () => {
return [withVShow(h('div'), this.value)]
}
})
app.mount(component, root)
render(h(component), root)
const $div = root.querySelector('div')
@@ -43,7 +42,7 @@ describe('runtime-dom: v-show directive', () => {
return [withVShow(h('div'), this.value)]
}
})
app.mount(component, root)
render(h(component), root)
const $div = root.querySelector('div')
@@ -59,7 +58,7 @@ describe('runtime-dom: v-show directive', () => {
return [withVShow(h('div'), this.value)]
}
})
app.mount(component, root)
render(h(component), root)
const $div = root.querySelector('div')
const data = root._vnode.component.data
@@ -110,7 +109,7 @@ describe('runtime-dom: v-show directive', () => {
]
}
})
app.mount(component, root)
render(h(component), root)
const $div = root.querySelector('div')
const data = root._vnode.component.data

View File

@@ -1,8 +1,8 @@
import {
createRenderer,
warn,
App,
RootRenderFunction
RootRenderFunction,
CreateAppFunction
} from '@vue/runtime-core'
import { nodeOps } from './nodeOps'
import { patchProp } from './patchProp'
@@ -17,8 +17,8 @@ const { render: baseRender, createApp: baseCreateApp } = createRenderer({
// use explicit type casts here to avoid import() calls in rolled-up d.ts
export const render = baseRender as RootRenderFunction<Node, Element>
export const createApp = (): App<Element> => {
const app = baseCreateApp()
export const createApp: CreateAppFunction<Element> = (...args) => {
const app = baseCreateApp(...args)
if (__DEV__) {
// Inject `isNativeTag`
@@ -29,8 +29,8 @@ export const createApp = (): App<Element> => {
})
}
const { mount, unmount } = app
app.mount = (component, container, props): any => {
const { mount } = app
app.mount = (container): any => {
if (isString(container)) {
container = document.querySelector(container)!
if (!container) {
@@ -39,6 +39,7 @@ export const createApp = (): App<Element> => {
return
}
}
const component = app.rootComponent
if (
__RUNTIME_COMPILE__ &&
!isFunction(component) &&
@@ -49,19 +50,7 @@ export const createApp = (): App<Element> => {
}
// clear content before mounting
container.innerHTML = ''
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 mount(container)
}
return app