从 vue 源码看问题 —— vue 编译器的解析(四)

简介: 从 vue 源码看问题 —— vue 编译器的解析

getSlotName、processSlotOutlet、processComponent


文件位置:/src/compiler/parser/index.js

getSlotName


/**
 * 解析 binding,得到插槽名称以及是否为动态插槽
 * @returns { name: 插槽名称, dynamic: 是否为动态插槽 }
 */
function getSlotName (binding) {
  let name = binding.name.replace(slotRE, '')
  if (!name) {
    if (binding.name[0] !== '#') {
      name = 'default'
    } else if (process.env.NODE_ENV !== 'production') {
      warn(
        `v-slot shorthand syntax requires a slot name.`,
        binding
      )
    }
  }
  return dynamicArgRE.test(name)
    // dynamic [name]
    ? { name: name.slice(1, -1), dynamic: true }
    // static name
    : { name: `"${name}"`, dynamic: false }
}
复制代码


processSlotOutlet


// handle <slot/> outlets,处理自闭合 slot 标签
// 得到插槽名称,el.slotName
function processSlotOutlet (el) {
  if (el.tag === 'slot') {
     // 得到插槽名称
    el.slotName = getBindingAttr(el, 'name')
     // 不允许在 slot 标签上使用 key 属性
    if (process.env.NODE_ENV !== 'production' && el.key) {
      warn(
        `\`key\` does not work on <slot> because slots are abstract outlets ` +
        `and can possibly expand into multiple elements. ` +
        `Use the key on a wrapping element instead.`,
        getRawBindingAttr(el, 'key')
      )
    }
  }
}
复制代码


processComponent


/**
 * 处理动态组件,<component :is="compName"></component>
 * 得到 el.component = compName
 */
function processComponent (el) {
  let binding
  // 解析 is 属性,得到属性值,即组件名称,el.component = compName
  if ((binding = getBindingAttr(el, 'is'))) {
    el.component = binding
  }
   /* 
    <component :is="compName" inline-template>xx</component>
    组件上存在 inline-template 属性,进行标记:el.inlineTemplate = true
    表示组件开始和结束标签内的内容作为组件模版出现,而不是作为插槽别分发,方便定义组件模版
   */
  if (getAndRemoveAttr(el, 'inline-template') != null) {
    el.inlineTemplate = true
  }
}
复制代码


transformNode() —— class 模块


文件位置:/src/platforms/web/compiler/modules/class.js


/**
 * 处理元素上的 class 属性
 * 静态的 class 属性值赋值给 el.staticClass 属性
 * 动态的 class 属性值赋值给 el.classBinding 属性
 */
function transformNode (el: ASTElement, options: CompilerOptions) {
  const warn = options.warn || baseWarn
  // 获取元素上静态 class 属性的值 xx,<div class="xx"></div>
  const staticClass = getAndRemoveAttr(el, 'class')
  if (process.env.NODE_ENV !== 'production' && staticClass) {
    const res = parseText(staticClass, options.delimiters)
    // 警告提示,同 style 的提示一样,不能使用 <div class="{{ val}}"></div>,请用
    // <div :class="val"></div> 代替
    if (res) {
      warn(
        `class="${staticClass}": ` +
        'Interpolation inside attributes has been removed. ' +
        'Use v-bind or the colon shorthand instead. For example, ' +
        'instead of <div class="{{ val }}">, use <div :class="val">.',
        el.rawAttrsMap['class']
      )
    }
  }
  // 静态 class 属性值赋值给 el.staticClass
  if (staticClass) {
    el.staticClass = JSON.stringify(staticClass.replace(/\s+/g, ' ').trim())
  }
  // 获取动态绑定的 class 属性值,并赋值给 el.classBinding
  const classBinding = getBindingAttr(el, 'class', false /* getStatic */)
  if (classBinding) {
    el.classBinding = classBinding
  }
}
复制代码


transformNode() —— style 模块


文件位置:/src/platforms/web/compiler/modules/style.js


/**
 * 从 el 上解析出静态的 style 属性和动态绑定的 style 属性,分别赋值给:
 * el.staticStyle 和 el.styleBinding
 */
function transformNode (el: ASTElement, options: CompilerOptions) {
  const warn = options.warn || baseWarn
   // <div style="xx"></div>
  // 获取 style 属性
  const staticStyle = getAndRemoveAttr(el, 'style')
  if (staticStyle) {
    /*
     istanbul ignore if 
     提示,如果从 xx 中解析到了界定符,说明是一个动态的 style,
     比如 <div style="{{ val }}"></div>则给出提示:
     动态的 style 请使用 <div :style="val"></div>
    */
    if (process.env.NODE_ENV !== 'production') {
      const res = parseText(staticStyle, options.delimiters)
      if (res) {
        warn(
          `style="${staticStyle}": ` +
          'Interpolation inside attributes has been removed. ' +
          'Use v-bind or the colon shorthand instead. For example, ' +
          'instead of <div style="{{ val }}">, use <div :style="val">.',
          el.rawAttrsMap['style']
        )
      }
    }
    // 将静态的 style 样式赋值给 el.staticStyle
    el.staticStyle = JSON.stringify(parseStyleText(staticStyle))
  }
  // 获取动态绑定的 style 属性,比如 <div :style="styleVariable"></div>
  const styleBinding = getBindingAttr(el, 'style', false /* getStatic */)
  if (styleBinding) {
    // 赋值给 el.styleBinding
    el.styleBinding = styleBinding
  }
}



目录
相关文章
|
11天前
|
移动开发 网络协议 安全
HTML5页面被运营商DNS问题及解决方案,app中h5页面源码的获取
HTML5页面被运营商DNS问题及解决方案,app中h5页面源码的获取
73 4
|
11天前
|
域名解析 网络协议 应用服务中间件
2024最新彩虹聚合DNS管理系统源码v1.3 全开源
聚合DNS管理系统可以实现在一个网站内管理多个平台的域名解析,目前已支持的域名平台有:阿里云、腾讯云、华为云、西部数码、DNSLA、CloudFlare。 本系统支持多用户,每个用户可分配不同的域名解析权限;支持API接口,支持获取域名独立DNS控制面板登录链接,方便各种IDC系统对接。
64 0
|
17天前
|
监控 安全 NoSQL
采用java+springboot+vue.js+uniapp开发的一整套云MES系统源码 MES制造管理系统源码
MES系统是一套具备实时管理能力,建立一个全面的、集成的、稳定的制造物流质量控制体系;对生产线、工艺、人员、品质、效率等多方位的监控、分析、改进,满足精细化、透明化、自动化、实时化、数据化、一体化管理,实现企业柔性化制造管理。
44 3
|
18天前
|
Linux 网络安全 Windows
网络安全笔记-day8,DHCP部署_dhcp搭建部署,源码解析
网络安全笔记-day8,DHCP部署_dhcp搭建部署,源码解析
|
18天前
HuggingFace Tranformers 源码解析(4)
HuggingFace Tranformers 源码解析
83 0
|
18天前
HuggingFace Tranformers 源码解析(3)
HuggingFace Tranformers 源码解析
74 0
|
18天前
|
开发工具 git
HuggingFace Tranformers 源码解析(2)
HuggingFace Tranformers 源码解析
36 0
|
18天前
|
并行计算
HuggingFace Tranformers 源码解析(1)
HuggingFace Tranformers 源码解析
33 0
|
20天前
PandasTA 源码解析(二十三)
PandasTA 源码解析(二十三)
48 0
|
20天前
PandasTA 源码解析(二十二)(3)
PandasTA 源码解析(二十二)
41 0

推荐镜像

更多