美文网首页
vue源码中的createElement

vue源码中的createElement

作者: CRJ997 | 来源:发表于2019-11-26 19:18 被阅读0次

    vue源码中有一个点不懂(在createElementNS函数中)
    document.createElement(namespace + ':' + tagName),第一次见到会带冒号的
    查了一下MDN上的createElement,然后发现不能这样写:

    MDN上的createElement解释
    不过根据这张图和下面的constructor里面的this.ns = undefined,貌似是要解决其他文档的兼容性的感觉 MDN createElement开头说明
    export default class VNode {
      // 这里省略一堆属性
      constructor (
        tag?: string,
        data?: VNodeData,
        children?: ?Array<VNode>,
        text?: string,
        elm?: Node,
        context?: Component,
        componentOptions?: VNodeComponentOptions,
        asyncFactory?: Function
      ) {
        this.tag = tag
        this.data = data
        this.children = children
        this.text = text
        this.elm = elm
        this.ns = undefined // constructor这里一开始把ns设置成了undefined,看起来上面的那个问题应该是为了兼容其他文档
        this.context = context
        this.fnContext = undefined
        this.fnOptions = undefined
        this.fnScopeId = undefined
        this.key = data && data.key
        this.componentOptions = componentOptions
        this.componentInstance = undefined
        this.parent = undefined
        this.raw = false
        this.isStatic = false
        this.isRootInsert = true
        this.isComment = false
        this.isCloned = false
        this.isOnce = false
        this.asyncFactory = asyncFactory
        this.asyncMeta = undefined
        this.isAsyncPlaceholder = false
      }
    
      // DEPRECATED: alias for componentInstance for backwards compat.
      /* istanbul ignore next */
      get child (): Component | void {
        return this.componentInstance
      }
    }
    
    

    不过实际上也有一个可以指定元素名称空间的createElementNS函数, 但是vue 2.0的诞生时间可能比这个函数早, 所以需要这样写

    相关文章

      网友评论

          本文标题:vue源码中的createElement

          本文链接:https://www.haomeiwen.com/subject/sygewctx.html