feat(runtime-dom): support using mount target innerHTML as template
This commit is contained in:
parent
ed29af7bea
commit
6a92bbd9c0
@ -18,7 +18,7 @@ export interface App<HostElement = any> {
|
||||
directive(name: string, directive: Directive): this
|
||||
mount(
|
||||
rootComponent: Component,
|
||||
rootContainer: HostElement,
|
||||
rootContainer: HostElement | string,
|
||||
rootProps?: Data
|
||||
): ComponentPublicInstance
|
||||
provide<T>(key: InjectionKey<T> | string, value: T): void
|
||||
@ -141,7 +141,7 @@ export function createAppAPI<HostNode, HostElement>(
|
||||
|
||||
mount(
|
||||
rootComponent: Component,
|
||||
rootContainer: string | HostElement,
|
||||
rootContainer: HostElement,
|
||||
rootProps?: Data
|
||||
): any {
|
||||
if (!isMounted) {
|
||||
|
@ -123,7 +123,7 @@ export interface RendererOptions<HostNode = any, HostElement = any> {
|
||||
|
||||
export type RootRenderFunction<HostNode, HostElement> = (
|
||||
vnode: VNode<HostNode, HostElement> | null,
|
||||
dom: HostElement | string
|
||||
dom: HostElement
|
||||
) => void
|
||||
|
||||
/**
|
||||
@ -1858,19 +1858,12 @@ export function createRenderer<
|
||||
}
|
||||
}
|
||||
|
||||
function render(vnode: HostVNode | null, rawContainer: HostElement | string) {
|
||||
let container: any = rawContainer
|
||||
if (isString(container)) {
|
||||
container = hostQuerySelector(container)
|
||||
if (!container) {
|
||||
if (__DEV__) {
|
||||
warn(
|
||||
`Failed to locate root container: ` + `querySelector returned null.`
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
const render: RootRenderFunction<
|
||||
HostNode,
|
||||
HostElement & {
|
||||
_vnode: HostVNode | null
|
||||
}
|
||||
> = (vnode, container) => {
|
||||
if (vnode == null) {
|
||||
if (container._vnode) {
|
||||
unmount(container._vnode, null, null, true)
|
||||
|
@ -1,27 +1,54 @@
|
||||
import { createRenderer } from '@vue/runtime-core'
|
||||
import { createRenderer, warn } from '@vue/runtime-core'
|
||||
import { nodeOps } from './nodeOps'
|
||||
import { patchProp } from './patchProp'
|
||||
// Importing from the compiler, will be tree-shaken in prod
|
||||
import { isHTMLTag, isSVGTag } from '@vue/compiler-dom'
|
||||
import { isFunction, isString } from '@vue/shared'
|
||||
|
||||
const { render, createApp } = createRenderer<Node, Element>({
|
||||
const { render, createApp: baseCreateApp } = createRenderer<Node, Element>({
|
||||
patchProp,
|
||||
...nodeOps
|
||||
})
|
||||
|
||||
const wrappedCreateApp = () => {
|
||||
const app = createApp()
|
||||
// inject `isNativeTag` dev only
|
||||
const createApp = () => {
|
||||
const app = baseCreateApp()
|
||||
|
||||
if (__DEV__) {
|
||||
// Inject `isNativeTag`
|
||||
// this is used for component name validation (dev only)
|
||||
Object.defineProperty(app.config, 'isNativeTag', {
|
||||
value: (tag: string) => isHTMLTag(tag) || isSVGTag(tag),
|
||||
writable: false
|
||||
})
|
||||
}
|
||||
|
||||
const mount = app.mount
|
||||
app.mount = (component, container, props): any => {
|
||||
if (isString(container)) {
|
||||
container = document.querySelector(container)!
|
||||
if (!container) {
|
||||
__DEV__ &&
|
||||
warn(`Failed to mount app: mount target selector returned null.`)
|
||||
return
|
||||
}
|
||||
}
|
||||
if (
|
||||
__RUNTIME_COMPILE__ &&
|
||||
!isFunction(component) &&
|
||||
!component.render &&
|
||||
!component.template
|
||||
) {
|
||||
component.template = container.innerHTML
|
||||
}
|
||||
// clear content before mounting
|
||||
container.innerHTML = ''
|
||||
return mount(component, container, props)
|
||||
}
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
const exportedCreateApp = __DEV__ ? wrappedCreateApp : createApp
|
||||
|
||||
export { render, exportedCreateApp as createApp }
|
||||
export { render, createApp }
|
||||
|
||||
// DOM-only runtime helpers
|
||||
export {
|
||||
|
Loading…
Reference in New Issue
Block a user