当前位置:

首页 > 编程开发 > vue3中的createApp怎么使用

vue3中的createApp怎么使用

函数定义createApp函数定义在文件packages/runtime-dom/src/index.ts中exportconstcreateApp=((...args)=>{constapp=ensureRenderer().createApp(...args)if(__DEV__){injectNativeTagCheck(app)injectCompilerOptionsCheck(app)}const{mount}=appapp.mount=(containerOrSelector:Element

本文目录

    函数定义

    createApp函数定义在文件 packages/runtime-dom/src/index.ts中

    export const createApp = ((...args) => {
      const app = ensureRenderer().createApp(...args)
     
      if (__DEV__) {
        injectNativeTagCheck(app)
        injectCompilerOptionsCheck(app)
      }
     
      const { mount } = app
      app.mount = (containerOrSelector: Element | ShadowRoot | string): any => {
        const container = normalizeContainer(containerOrSelector)
        if (!container) return
     
        const component = app._component
        if (!isFunction(component) && !component.render && !component.template) {
          // __UNSAFE__
          // Reason: potential execution of JS expressions in in-DOM template.
          // The user must make sure the in-DOM template is trusted. If it's
          // rendered by the server, the template should not contain any user data.
          component.template = container.innerHTML
          // 2.x compat check
          if (__COMPAT__ && __DEV__) {
            for (let i = 0; i < container.attributes.length; i++) {
              const attr = container.attributes[i]
              if (attr.name !== 'v-cloak' && /^(v-|:|@)/.test(attr.name)) {
                compatUtils.warnDeprecation(
                  DeprecationTypes.GLOBAL_MOUNT_CONTAINER,
                  null
                )
                break
              }
            }
          }
        }
     
        // clear content before mounting
        container.innerHTML = ''
        const proxy = mount(container, false, container instanceof SVGElement)
        if (container instanceof Element) {
          container.removeAttribute('v-cloak')
          container.setAttribute('data-v-app', '')
        }
        return proxy
      }
     
      return app
    }) as CreateAppFunction

    (1)首先创建App对象

    (2)取出app对象中的mount方法,重写mount方法

    • 首先调用 normalizeContainer 函数来获取container节点

    • 清空container的innerHTML

    • 调用原mount方法

    ensureRenderer

    其函数定义为

    function ensureRenderer() {
      return (
        renderer ||
        (renderer = createRenderer(rendererOptions))
      )
    }

    ensureRenderer通过调用createRenderer,createRenderer函数定义在 packages/runtime-core/src/renderer.ts文件中

    export function createRenderer<
      HostNode = RendererNode,
      HostElement = RendererElement
    >(options: RendererOptions) {
      return baseCreateRenderer(options)
    }

    最终是调用baseCreateRenderer,其最终返回

    return {
        render,
        hydrate,
        createApp: createAppAPI(render, hydrate)
      }

    createAppAPI

    createAppAPI函数定义在 packages/runtime-core/src/apiCreateApp.ts文件中,其返回函数

    return function createApp(rootComponent, rootProps = null)

    mount

    其定义在packages/runtime-core/src/apiCreateApp.ts文件

    mount(
            rootContainer: HostElement,
            isHydrate?: boolean,
            isSVG?: boolean
          ): any {
            if (!isMounted) {
              const vnode = createVNode(
                rootComponent as ConcreteComponent,
                rootProps
              )
              // store app context on the root VNode.
              // this will be set on the root instance on initial mount.
              vnode.appContext = context
     
              // HMR root reload
              if (__DEV__) {
                context.reload = () => {
                  render(cloneVNode(vnode), rootContainer, isSVG)
                }
              }
     
              if (isHydrate && hydrate) {
                hydrate(vnode as VNode, rootContainer as any)
              } else {
                render(vnode, rootContainer, isSVG)
              }
              isMounted = true
              app._container = rootContainer
              // for devtools and telemetry
              ;(rootContainer as any).__vue_app__ = app
     
              if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
                app._instance = vnode.component
                devtoolsInitApp(app, version)
              }
     
              return vnode.component!.proxy
            } else if (__DEV__) {
              warn(
                `App has already been mounted.\n` +
                  `If you want to remount the same app, move your app creation logic ` +
                  `into a factory function and create fresh app instances for each ` +
                  `mount - e.g. \`const createMyApp = () => createApp(App)\``
              )
            }
          }

    (1)调用createVNode创建虚拟结点

    (2)调用 render 函数来实现渲染vnode。render函数是baseCreateRenderer 函数返回调用 createAppAPI 时传入的参数之一

    // implementation
    function baseCreateRenderer(
      options: RendererOptions,
      createHydrationFns?: typeof createHydrationFunctions
    ): any {
        ....
      const render: RootRenderFunction = (vnode, container, isSVG) => {
        if (vnode == null) {
          if (container._vnode) {
            unmount(container._vnode, null, null, true)
          }
        } else {
          patch(container._vnode || null, vnode, container, null, null, null, isSVG)
        }
        flushPostFlushCbs()
        container._vnode = vnode
      }
     .....
    }

    patch

    函数定义在 packages/runtime-core/src/renderer.ts文件中

    const patch: PatchFn = (
        n1,
        n2,
        container,
        anchor = null,
        parentComponent = null,
        parentSuspense = null,
        isSVG = false,
        slotScopeIds = null,
        optimized = __DEV__ && isHmrUpdating ? false : !!n2.dynamicChildren
      ) => {
        // patching & not same type, unmount old tree
        if (n1 && !isSameVNodeType(n1, n2)) {
          anchor = getNextHostNode(n1)
          unmount(n1, parentComponent, parentSuspense, true)
          n1 = null
        }
     
        if (n2.patchFlag === PatchFlags.BAIL) {
          optimized = false
          n2.dynamicChildren = null
        }
     
        const { type, ref, shapeFlag } = n2
        switch (type) {
          case Text:
            processText(n1, n2, container, anchor)
            break
          case Comment:
            processCommentNode(n1, n2, container, anchor)
            break
          case Static:
            if (n1 == null) {
              mountStaticNode(n2, container, anchor, isSVG)
            } else if (__DEV__) {
              patchStaticNode(n1, n2, container, isSVG)
            }
            break
          case Fragment:
            processFragment(
              n1,
              n2,
              container,
              anchor,
              parentComponent,
              parentSuspense,
              isSVG,
              slotScopeIds,
              optimized
            )
            break
          default:
            if (shapeFlag & ShapeFlags.ELEMENT) {
              processElement(
                n1,
                n2,
                container,
                anchor,
                parentComponent,
                parentSuspense,
                isSVG,
                slotScopeIds,
                optimized
              )
            } else if (shapeFlag & ShapeFlags.COMPONENT) {
              processComponent(
                n1,
                n2,
                container,
                anchor,
                parentComponent,
                parentSuspense,
                isSVG,
                slotScopeIds,
                optimized
              )
            } else if (shapeFlag & ShapeFlags.TELEPORT) {
              ;(type as typeof TeleportImpl).process(
                n1 as TeleportVNode,
                n2 as TeleportVNode,
                container,
                anchor,
                parentComponent,
                parentSuspense,
                isSVG,
                slotScopeIds,
                optimized,
                internals
              )
            } else if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
              ;(type as typeof SuspenseImpl).process(
                n1,
                n2,
                container,
                anchor,
                parentComponent,
                parentSuspense,
                isSVG,
                slotScopeIds,
                optimized,
                internals
              )
            } else if (__DEV__) {
              warn('Invalid VNode type:', type, `(${typeof type})`)
            }
        }
     
        // set ref
        if (ref != null && parentComponent) {
          setRef(ref, n1 && n1.ref, parentSuspense, n2 || n1, !n2)
        }
      }
    const patch: PatchFn = (
        n1,
        n2,
        container,
        anchor = null,
        parentComponent = null,
        parentSuspense = null,
        isSVG = false,
        slotScopeIds = null,
        optimized = __DEV__ && isHmrUpdating ? false : !!n2.dynamicChildren
      ) => {
        // patching & not same type, unmount old tree
        if (n1 && !isSameVNodeType(n1, n2)) {
          anchor = getNextHostNode(n1)
          unmount(n1, parentComponent, parentSuspense, true)
          n1 = null
        }
     
        if (n2.patchFlag === PatchFlags.BAIL) {
          optimized = false
          n2.dynamicChildren = null
        }
     
        const { type, ref, shapeFlag } = n2
        switch (type) {
          case Text:
            processText(n1, n2, container, anchor)
            break
          case Comment:
            processCommentNode(n1, n2, container, anchor)
            break
          case Static:
            if (n1 == null) {
              mountStaticNode(n2, container, anchor, isSVG)
            } else if (__DEV__) {
              patchStaticNode(n1, n2, container, isSVG)
            }
            break
          case Fragment:
            processFragment(
              n1,
              n2,
              container,
              anchor,
              parentComponent,
              parentSuspense,
              isSVG,
              slotScopeIds,
              optimized
            )
            break
          default:
            if (shapeFlag & ShapeFlags.ELEMENT) {
              processElement(
                n1,
                n2,
                container,
                anchor,
                parentComponent,
                parentSuspense,
                isSVG,
                slotScopeIds,
                optimized
              )
            } else if (shapeFlag & ShapeFlags.COMPONENT) {
              processComponent(
                n1,
                n2,
                container,
                anchor,
                parentComponent,
                parentSuspense,
                isSVG,
                slotScopeIds,
                optimized
              )
            } else if (shapeFlag & ShapeFlags.TELEPORT) {
              ;(type as typeof TeleportImpl).process(
                n1 as TeleportVNode,
                n2 as TeleportVNode,
                container,
                anchor,
                parentComponent,
                parentSuspense,
                isSVG,
                slotScopeIds,
                optimized,
                internals
              )
            } else if (__FEATURE_SUSPENSE__ && shapeFlag & ShapeFlags.SUSPENSE) {
              ;(type as typeof SuspenseImpl).process(
                n1,
                n2,
                container,
                anchor,
                parentComponent,
                parentSuspense,
                isSVG,
                slotScopeIds,
                optimized,
                internals
              )
            } else if (__DEV__) {
              warn('Invalid VNode type:', type, `(${typeof type})`)
            }
        }
     
        // set ref
        if (ref != null && parentComponent) {
          setRef(ref, n1 && n1.ref, parentSuspense, n2 || n1, !n2)
        }
      }

    processComponent

    函数定义在packages/runtime-core/src/renderer.ts文件中

    const processComponent = (
        n1: VNode | null,
        n2: VNode,
        container: RendererElement,
        anchor: RendererNode | null,
        parentComponent: ComponentInternalInstance | null,
        parentSuspense: SuspenseBoundary | null,
        isSVG: boolean,
        slotScopeIds: string[] | null,
        optimized: boolean
      ) => {
        n2.slotScopeIds = slotScopeIds
        if (n1 == null) {
          if (n2.shapeFlag & ShapeFlags.COMPONENT_KEPT_ALIVE) {
            ;(parentComponent!.ctx as KeepAliveContext).activate(
              n2,
              container,
              anchor,
              isSVG,
              optimized
            )
          } else {
            mountComponent(
              n2,
              container,
              anchor,
              parentComponent,
              parentSuspense,
              isSVG,
              optimized
            )
          }
        } else {
          updateComponent(n1, n2, optimized)
        }
      }

    比较旧的虚拟结点n1和新的虚拟节点n2,如果n1为空则挂载结点,否则更新结点。此时n1为空,执行mountComponent挂载结点

    mountComponent

    函数定义在packages/runtime-core/src/renderer.ts文件中

    const mountComponent: MountComponentFn = (
        initialVNode,
        container,
        anchor,
        parentComponent,
        parentSuspense,
        isSVG,
        optimized
      ) => {
        // 2.x compat may pre-creaate the component instance before actually
        // mounting
        const compatMountInstance =
          __COMPAT__ && initialVNode.isCompatRoot && initialVNode.component
        const instance: ComponentInternalInstance =
          compatMountInstance ||
          (initialVNode.component = createComponentInstance(
            initialVNode,
            parentComponent,
            parentSuspense
          ))
     
        // inject renderer internals for keepAlive
        if (isKeepAlive(initialVNode)) {
          ;(instance.ctx as KeepAliveContext).renderer = internals
        }
     
        // resolve props and slots for setup context
        if (!(__COMPAT__ && compatMountInstance)) {
          setupComponent(instance)
        }
     
        // setup() is async. This component relies on async logic to be resolved
        // before proceeding
        if (__FEATURE_SUSPENSE__ && instance.asyncDep) {
          parentSuspense && parentSuspense.registerDep(instance, setupRenderEffect)
     
          // Give it a placeholder if this is not hydration
          // TODO handle self-defined fallback
          if (!initialVNode.el) {
            const placeholder = (instance.subTree = createVNode(Comment))
            processCommentNode(null, placeholder, container!, anchor)
          }
          return
        }
     
        setupRenderEffect(
          instance,
          initialVNode,
          container,
          anchor,
          parentSuspense,
          isSVG,
          optimized
        )
      }

    其执行流程为

    (1)首先调用createComponentInstance创建组件的实例对象

    (2)调用setupComponent来初始化属性及slots属性

    (3)调用设置和渲染有副作用的函数setupRenderEffect,主要是执行ReactiveEffect的run方法来执行componentUpdateFn以及调度queueJob。

    setupRenderEffect

    函数定义在packages/runtime-core/src/renderer.ts文件中,关键的是当中定义的函数componentUpdateFn

    const componentUpdateFn = () => {
          if (!instance.isMounted) {
            let vnodeHook: VNodeHook | null | undefined
            const { el, props } = initialVNode
            const { bm, m, parent } = instance
     
            effect.allowRecurse = false
            // beforeMount hook
            if (bm) {
              invokeArrayFns(bm)
            }
            // onVnodeBeforeMount
            if ((vnodeHook = props && props.onVnodeBeforeMount)) {
              invokeVNodeHook(vnodeHook, parent, initialVNode)
            }
            if (
              __COMPAT__ &&
              isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS, instance)
            ) {
              instance.emit('hook:beforeMount')
            }
            effect.allowRecurse = true
     
            if (el && hydrateNode) {
              // vnode has adopted host node - perform hydration instead of mount.
              const hydrateSubTree = () => {
        
                instance.subTree = renderComponentRoot(instance)
                
                hydrateNode!(
                  el as Node,
                  instance.subTree,
                  instance,
                  parentSuspense,
                  null
                )
                
              }
     
              if (isAsyncWrapper(initialVNode)) {
                (initialVNode.type as ComponentOptions).__asyncLoader!().then(
                  // note: we are moving the render call into an async callback,
                  // which means it won't track dependencies - but it's ok because
                  // a server-rendered async wrapper is already in resolved state
                  // and it will never need to change.
                  () => !instance.isUnmounted && hydrateSubTree()
                )
              } else {
                hydrateSubTree()
              }
            } else {
              
              const subTree = (instance.subTree = renderComponentRoot(instance))
             
              patch(
                null,
                subTree,
                container,
                anchor,
                instance,
                parentSuspense,
                isSVG
              )
              
              initialVNode.el = subTree.el
            }
            // mounted hook
            if (m) {
              queuePostRenderEffect(m, parentSuspense)
            }
            // onVnodeMounted
            if ((vnodeHook = props && props.onVnodeMounted)) {
              const scopedInitialVNode = initialVNode
              queuePostRenderEffect(
                () => invokeVNodeHook(vnodeHook!, parent, scopedInitialVNode),
                parentSuspense
              )
            }
            if (
              __COMPAT__ &&
              isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS, instance)
            ) {
              queuePostRenderEffect(
                () => instance.emit('hook:mounted'),
                parentSuspense
              )
            }
     
            // activated hook for keep-alive roots.
            // #1742 activated hook must be accessed after first render
            // since the hook may be injected by a child keep-alive
            if (initialVNode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE) {
              instance.a && queuePostRenderEffect(instance.a, parentSuspense)
              if (
                __COMPAT__ &&
                isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS, instance)
              ) {
                queuePostRenderEffect(
                  () => instance.emit('hook:activated'),
                  parentSuspense
                )
              }
            }
            instance.isMounted = true
            // #2458: deference mount-only object parameters to prevent memleaks
            initialVNode = container = anchor = null as any
          } else {
            // updateComponent
            // This is triggered by mutation of component's own state (next: null)
            // OR parent calling processComponent (next: VNode)
            let { next, bu, u, parent, vnode } = instance
            let originNext = next
            let vnodeHook: VNodeHook | null | undefined
     
            if (next) {
              next.el = vnode.el
              updateComponentPreRender(instance, next, optimized)
            } else {
              next = vnode
            }
     
            // Disallow component effect recursion during pre-lifecycle hooks.
            effect.allowRecurse = false
            // beforeUpdate hook
            if (bu) {
              invokeArrayFns(bu)
            }
            // onVnodeBeforeUpdate
            if ((vnodeHook = next.props && next.props.onVnodeBeforeUpdate)) {
              invokeVNodeHook(vnodeHook, parent, next, vnode)
            }
            if (
              __COMPAT__ &&
              isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS, instance)
            ) {
              instance.emit('hook:beforeUpdate')
            }
            effect.allowRecurse = true
     
            // render
            const nextTree = renderComponentRoot(instance)
            const prevTree = instance.subTree
            instance.subTree = nextTree
            patch(
              prevTree,
              nextTree,
              // parent may have changed if it's in a teleport
              hostParentNode(prevTree.el!)!,
              // anchor may have changed if it's in a fragment
              getNextHostNode(prevTree),
              instance,
              parentSuspense,
              isSVG
            )
         
            next.el = nextTree.el
            if (originNext === null) {
              // self-triggered update. In case of HOC, update parent component
              // vnode el. HOC is indicated by parent instance's subTree pointing
              // to child component's vnode
              updateHOCHostEl(instance, nextTree.el)
            }
            // updated hook
            if (u) {
              queuePostRenderEffect(u, parentSuspense)
            }
            // onVnodeUpdated
            if ((vnodeHook = next.props && next.props.onVnodeUpdated)) {
              queuePostRenderEffect(
                () => invokeVNodeHook(vnodeHook!, parent, next!, vnode),
                parentSuspense
              )
            }
            if (
              __COMPAT__ &&
              isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS, instance)
            ) {
              queuePostRenderEffect(
                () => instance.emit('hook:updated'),
                parentSuspense
              )
            }
          }
        }

    如果组件没有挂载,则挂载组件,并在挂载完把组件的 isMounted 属性设置为true,表示已挂载。如果已经挂载,则更新组件。

    递归调用 patch 函数挂载组件

    processFragment

    函数定义在packages/runtime-core/src/renderer.ts文件中

    const processFragment = (
        n1: VNode | null,
        n2: VNode,
        container: RendererElement,
        anchor: RendererNode | null,
        parentComponent: ComponentInternalInstance | null,
        parentSuspense: SuspenseBoundary | null,
        isSVG: boolean,
        slotScopeIds: string[] | null,
        optimized: boolean
      ) => {
        const fragmentStartAnchor = (n2.el = n1 ? n1.el : hostCreateText(''))!
        const fragmentEndAnchor = (n2.anchor = n1 ? n1.anchor : hostCreateText(''))!
     
        let { patchFlag, dynamicChildren, slotScopeIds: fragmentSlotScopeIds } = n2
        if (dynamicChildren) {
          optimized = true
        }
     
        // check if this is a slot fragment with :slotted scope ids
        if (fragmentSlotScopeIds) {
          slotScopeIds = slotScopeIds
            ? slotScopeIds.concat(fragmentSlotScopeIds)
            : fragmentSlotScopeIds
        }
     
        if (n1 == null) {
          hostInsert(fragmentStartAnchor, container, anchor)
          hostInsert(fragmentEndAnchor, container, anchor)
          // a fragment can only have array children
          // since they are either generated by the compiler, or implicitly created
          // from arrays.
          mountChildren(
            n2.children as VNodeArrayChildren,
            container,
            fragmentEndAnchor,
            parentComponent,
            parentSuspense,
            isSVG,
            slotScopeIds,
            optimized
          )
        } else {
          if (
            patchFlag > 0 &&
            patchFlag & PatchFlags.STABLE_FRAGMENT &&
            dynamicChildren &&
            // #2715 the previous fragment could've been a BAILed one as a result
            // of renderSlot() with no valid children
            n1.dynamicChildren
          ) {
            // a stable fragment (template root or ) doesn't need to
            // patch children order, but it may contain dynamicChildren.
            patchBlockChildren(
              n1.dynamicChildren,
              dynamicChildren,
              container,
              parentComponent,
              parentSuspense,
              isSVG,
              slotScopeIds
            )
            if (__DEV__ && parentComponent && parentComponent.type.__hmrId) {
              traverseStaticChildren(n1, n2)
            } else if (
              // #2080 if the stable fragment has a key, it's a  that may
              //  get moved around. Make sure all root level vnodes inherit el.
              // #2134 or if it's a component root, it may also get moved around
              // as the component is being moved.
              n2.key != null ||
              (parentComponent && n2 === parentComponent.subTree)
            ) {
              traverseStaticChildren(n1, n2, true /* shallow */)
            }
          } else {
            // keyed / unkeyed, or manual fragments.
            // for keyed & unkeyed, since they are compiler generated from v-for,
            // each child is guaranteed to be a block so the fragment will never
            // have dynamicChildren.
            patchChildren(
              n1,
              n2,
              container,
              fragmentEndAnchor,
              parentComponent,
              parentSuspense,
              isSVG,
              slotScopeIds,
              optimized
            )
          }
        }
      }

    (1)如果n1为空,则执行mountChildren

    (2)否则执行patchChildren

    mountChildren

    函数定义在packages/runtime-core/src/renderer.ts文件中

      const mountChildren: MountChildrenFn = (
        children,
        container,
        anchor,
        parentComponent,
        parentSuspense,
        isSVG,
        slotScopeIds,
        optimized,
        start = 0
      ) => {
        for (let i = start; i < children.length; i++) {
          const child = (children[i] = optimized
            ? cloneIfMounted(children[i] as VNode)
            : normalizeVNode(children[i]))
          patch(
            null,
            child,
            container,
            anchor,
            parentComponent,
            parentSuspense,
            isSVG,
            slotScopeIds,
            optimized
          )
        }
      }

    遍历children,执行patch挂载所有子元素

    processElement

    函数定义在packages/runtime-core/src/renderer.ts文件中

    const processElement = (
        n1: VNode | null,
        n2: VNode,
        container: RendererElement,
        anchor: RendererNode | null,
        parentComponent: ComponentInternalInstance | null,
        parentSuspense: SuspenseBoundary | null,
        isSVG: boolean,
        slotScopeIds: string[] | null,
        optimized: boolean
      ) => {
        isSVG = isSVG || (n2.type as string) === 'svg'
        if (n1 == null) {
          mountElement(
            n2,
            container,
            anchor,
            parentComponent,
            parentSuspense,
            isSVG,
            slotScopeIds,
            optimized
          )
        } else {
          patchElement(
            n1,
            n2,
            parentComponent,
            parentSuspense,
            isSVG,
            slotScopeIds,
            optimized
          )
        }
      }

    (1)如果n1为空,则调用mountElement挂载元素

    (2)如果n1不为空,则调用patchElement来更新元素

    此时,执行挂载元素。

    mountElement

    函数定义在packages/runtime-core/src/renderer.ts文件中

    const processElement = (
        n1: VNode | null,
        n2: VNode,
        container: RendererElement,
        anchor: RendererNode | null,
        parentComponent: ComponentInternalInstance | null,
        parentSuspense: SuspenseBoundary | null,
        isSVG: boolean,
        slotScopeIds: string[] | null,
        optimized: boolean
      ) => {
        isSVG = isSVG || (n2.type as string) === 'svg'
        if (n1 == null) {
          mountElement(
            n2,
            container,
            anchor,
            parentComponent,
            parentSuspense,
            isSVG,
            slotScopeIds,
            optimized
          )
        } else {
          patchElement(
            n1,
            n2,
            parentComponent,
            parentSuspense,
            isSVG,
            slotScopeIds,
            optimized
          )
        }
      }

    (1)调用hostCreateElement根据类型和其他属性,创建DOM元素节点

    (2)判断子节点的类型,若子节点为纯文本,则直接处理纯文本hostSetElementText,若子节点为数组,则调用 mountChildren 函数深度遍历处理子节点

    (3)处理 props 属性,主要是调用hostPatchProp和setScopeId

    (4)调用hostInsert将el挂载到 container 中

    本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系bd@zhengruan.com
    作者最新文章
    编程开发 Vue3
    相关文章 更多
    pinia数据持久化的步骤详解与插件配置教程
    pinia数据持久化的步骤详解与插件配置教程

    详解Pinia数据持久化的实现步骤,包括手动订阅状态、封装通用插件及处理序列化例外,帮助开发者构建稳定的前端状态存储方案。

    如何从零开始搭建vue3项目
    如何从零开始搭建vue3项目

    说明记录一次Vue3的项目搭建过程。文章基于vue3.2.6和vite2.51版本,使用了ui库Elementplus,vue-router4,Layout布局封装,axios请求封装,别名配置等。开始1.使用vscode开发工具安装vue3的插件Volar,在vue2中我们使用的是Vetur。vue3在线code工具传送门sfc.vuejs.org/2.执行初始化及安装命令:npminitvite初始化vite此过程可以输入项目名、选择vue/react项目及js/ts环境选择,vue3已经完全支持ts

    vue3中getCurrentInstance怎么使用
    vue3中getCurrentInstance怎么使用

    父组件中:1.setup语法糖中导入子组件2.在子组件标签上绑定ref值3.setup内部从vue中按需导出getCurrentInstance方法4.调用getCurrentInstance方法导出proxy5.通过proxy.$refs.子组件ref名.子组件内属性/方法实现调用子组件count+1import{getCurrentInstance,ComponetInternalInstance,ref}from"vue";importChildfrom"./zi.vue";constchild=r

    怎么使用vue3搭建后台系统
    怎么使用vue3搭建后台系统

    首先使用npm或者yarn创建一个vue项目//使用npm创建一个基于vite构建的vue项目npmcreatevite@latest//使用yarn创建一个基于vite构建的vue项目yarncreatevite@latest在创建的构成中选择vuevue-ts创建完之后将项目拖到编译器打开一、配置vite在vite.config.ts文件中配置项目的服务数据,配置如下://此处配置项目服务参数server:{host:"0.0.0.0",//项目运行地址,此处代表localhostport:8888,

    Vue3基于countUp.js怎么实现数字滚动的插件
    Vue3基于countUp.js怎么实现数字滚动的插件

    countUp简介CountUp.js是一种无依赖项的轻量级JavaScript类,可用于快速创建以更有趣的方式显示数字数据的动画。CountUp可以在两个方向上进行计数,具体取决于传递的开始和结束值。虽然现在市面上基于countUp.js二次封装的Vue组件不在少数,但我个人是不太喜欢使用这些第三方封装的,因为第三方组件的更新频率很难保证,也许作者只是一时兴起封装上传了,并未打算继续维护,如果使用了等于后续根本没有维护性了,所以这种二次封装我推荐自行实现,我们可以通过本次封装熟悉一下vue3,ts的语法

    vue3中addRoute路由变化但页面未更新如何解决
    vue3中addRoute路由变化但页面未更新如何解决

    解决办法,每次addRoute时深复制组件对象importtimefrom"../views/time"functioncopyObj(obj){if(typeofobj=="object"){if(Array.isArray(obj)){letarr=[];for(letitemofobj){arr.push(Object.assign(copyObj(item)));}returnarr;}elseif(obj==null){returnnull;}else{letobj1={};for(letind

    怎么将Vue3 Axios拦截器封装成request文件
    怎么将Vue3 Axios拦截器封装成request文件

    1、创建一个名为request.js的新文件,并导入Axios:importaxiosfrom'axios';2、创建一个名为request的函数,并将其导出:这将创建一个名为request的函数,并将其设置为具有基本URL的新的Axios实例。要在封装的Axios实例中添加超时设置,可以在创建Axios实例时传递timeout选项。exportconstrequest=axios.create({baseURL:'https://example.com/api',time

    Vue3侦听器的实现原理是什么
    Vue3侦听器的实现原理是什么

    侦听响应式对象前面我们聊到计算属性,它可以自动计算并缓存响应式数据的值。而如果我们仅需要在响应式数据变化时,执行一些预设的操作,就可以使用watch侦听器。我们还是先来实现一个最简单的例子,然后来一点一点扩充它。constdata={foo:1}constobj=reactive(data)watch(obj,()=>{console.log('obj已改变')})在这个例子中,我们使用了watch侦听器,当obj的属性被改变时,控制台应该会打印出obj已改变。基于前面我们对计算属性的实现

    vue3中<script setup>和setup函数的区别是什么
    vue3中<script setup>和setup函数的区别是什么

    是在单文件组件(SFC)中使用组合式API的编译时语法糖。当同时使用SFC与组合式API时该语法是默认推荐。相比于普通的语法,它具有更多优势:更少的样板内容,更简洁的代码。能够使用纯TypeScript声明props和自定义事件。更好的运行时性能(其模板会被编译成同一作用域内的渲染函数,避免了渲染上下文代理对象)。更好的IDE类型推导性能(减少了语言服务器从代码中抽取类型的工作)。以上是vue3官网对的说明,其实<scriptsetup>就是setup函数的一个语法糖。一、基本语法setup函数的写法im

    Vue3进阶主题Composition API如何使用
    Vue3进阶主题Composition API如何使用

    什么是CompositionAPICompositionAPI是Vue3中引入的一种新的API风格,旨在提高代码的可读性、可维护性和可重用性。CompositionAPI不同于Vue2中的OptionsAPI,它采用了一种基于函数的编程方式,将组件内的逻辑分解成更小的可组合函数单元,以实现更加灵活和高效的代码组织方式。为什么Vue3推荐使用CompositionAPIVue3推荐使用CompositionAPI的主要原因是为了更好地组织和重用组件逻辑。在Vue2中,我们通常使用OptionsAPI,其中我

    查看更多
    精品专题 更多
    装机必备
    装机必备

    正软商城装机必备专区,精选办公、浏览器、安全防护、影音播放、压缩解压、设计创作和系统工具等电脑常用正版软件,帮助用户快速完成新电脑软件配置。

    Windows
    Windows

    正软商城Windows软件专区,汇集适用于Windows电脑的办公、设计、安全防护、影音播放、开发工具和系统优化软件,提供软件介绍、系统要求、正版授权及购买下载服务。

    PDF教程
    PDF教程

    正软商城PDF教程频道提供PDF编辑、转换、合并、拆分、压缩及格式处理方法,同时介绍常用PDF软件和工具的使用技巧。

    Mac软件 更多
    讯飞智作 网页版
    讯飞智作 网页版
    Windows/ macOS/ Linux

    讯飞智作是一款基于语音合成技术、虚拟人驱动技术与数字人形象声音构建能力的 AI 数字人音视频创作平台。

    火山引擎 网页版
    火山引擎 网页版
    Windows/ macOS/ Linux

    火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。

    Aperty macOS版
    Aperty macOS版
    macOS

    Aperty 是 Skylum 推出的 AI 人像修图软件,支持自然肤质修饰、瑕疵处理、面部与身体调整、预设及批量同步。

    WINDOWS 更多
    讯飞智作 网页版
    讯飞智作 网页版
    Windows/ macOS/ Linux

    讯飞智作是一款基于语音合成技术、虚拟人驱动技术与数字人形象声音构建能力的 AI 数字人音视频创作平台。

    火山引擎 网页版
    火山引擎 网页版
    Windows/ macOS/ Linux

    火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。

    PixTV 网页版
    PixTV 网页版
    Windows

    PixTV 是一款在浏览器中使用的 AI 视觉内容创作平台,提供图片、视频、音频、文字生成、AI 分镜、局部重绘、提示词反推和无限画布等工具,适合短剧、漫剧、广告及社交媒体内容制作。