vue源码学习-1.Vue实例挂载的实现

 

 

通常我们开发都是单h文件组件, 借助webpack的vue-loader, 将.vue文件里面的template编译成render函数, 这是离线时做的, 而接下来为了着重分析带compiler版本的vue, 先来看一下src/platform/web/entry-runtime-with-compiler.js 文件中定首先el不能是body html 因为挂载会替换掉整个元素, 如果能挂载再body html上面势必会造成文档的错误, 接下来我们可以看到注释// resolve template/el and convert to render function,当不存在render函数时, 将template/el 转换成render 函数, 在这里我们可以知道当没有render函数时 vue会优先去寻找配置项上的template(编译成模板的优先级是template > el)并且template还支持多种写法

1.当template时字符串时, vue会认为用户写的是id选择器, 那么就调用idToTemplate, 将其转化成模板html字符串

2.如果template是DOM节点,那么直接读取它的innerHTML

当template没有写的时候,则会拿el的outerHTML当模板之后

const mount = Vue.prototype.$mount
Vue.prototype.$mount = function (
  el?: string | Element,
  hydrating?: boolean
): Component {
  el = el && query(el)

  /* istanbul ignore if */
  if (el === document.body || el === document.documentElement) {
    process.env.NODE_ENV !== 'production' && warn(
      `Do not mount Vue to <html> or <body> - mount to normal elements instead.`
    )
    return this
  }

  const options = this.$options
  // resolve template/el and convert to render function
  if (!options.render) {
    let template = options.template
    if (template) {
      if (typeof template === 'string') {
        if (template.charAt(0) === '#') {
          template = idToTemplate(template)
          /* istanbul ignore if */
          if (process.env.NODE_ENV !== 'production' && !template) {
            warn(
              `Template element not found or is empty: ${options.template}`,
              this
            )
          }
        }
      } else if (template.nodeType) {
        template = template.innerHTML
      } else {
        if (process.env.NODE_ENV !== 'production') {
          warn('invalid template option:' + template, this)
        }
        return this
      }
    } else if (el) {
      template = getOuterHTML(el)
    }
    if (template) {
      /* istanbul ignore if */
      if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
        mark('compile')
      }

      const { render, staticRenderFns } = compileToFunctions(template, {
        shouldDecodeNewlines,
        shouldDecodeNewlinesForHref,
        delimiters: options.delimiters,
        comments: options.comments
      }, this)
      options.render = render
      options.staticRenderFns = staticRenderFns

      /* istanbul ignore if */
      if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
        mark('compile end')
        measure(`vue ${this._name} compile`, 'compile', 'compile end')
      }
    }
  }
  return mount.call(this, el, hydrating)
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
vue-socket.io是基于socket.io-client的封装,它允许在Vue项目中更方便地使用实时、双向和基于事件的通信。它通过将$socket挂载Vue实例上,并使用sockets对象来实现组件化的事件监听。 在使用vue-socket.io时,你可以使用Vue.use(VueSocketio, 'http://socketserver.com:1923')来引入vue-socket.io插件。但需要注意,据引用的说法,这种方法可能存在一些问题,所以你可能需要注意一下。 如果你想深入了解vue-socket.io的使用,引用提供了一篇关于vue-socket.io使用的文章,其中包含了一些关于socket连接、断开重连以及动态连接地址的注意事项。你可以阅读这篇文章来获得更全面的了解。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Vue中 引入使用 vue-socket.io](https://blog.csdn.net/ZYS10000/article/details/122737466)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [最详细的vue-socket.io用法](https://blog.csdn.net/weixin_43898997/article/details/122962888)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值