美文网首页让前端飞JavaScript 进阶营
Vue内部怎样处理props选项的多种写法

Vue内部怎样处理props选项的多种写法

作者: 边城少年_ | 来源:发表于2018-11-05 15:19 被阅读4次

开发过程中,props 的使用有两种写法:

// 字符串数组写法
const subComponent = {
  props: ['name']
}
// 对象写法
const subComponent = {
  props: {
    name: {
      type: String,
      default: 'Kobe Bryant'
    }
  }
}

Vue在内部会对 props 选项进行处理,无论开发时使用了哪种语法,Vue都会将其规范化为对象的形式。具体规范方式见Vue源码 src/core/util/options.js 文件中的 normalizeProps 函数:

/**
 * Ensure all props option syntax are normalized into the
 * Object-based format.(确保将所有props选项语法规范为基于对象的格式)
 */
 // 参数的写法为 flow(https://flow.org/) 语法
function normalizeProps (options: Object, vm: ?Component) {
  const props = options.props
  // 如果选项中没有props,那么直接return
  if (!props) return
  // 如果有,开始对其规范化
  // 声明res,用于保存规范化后的结果
  const res = {}
  let i, val, name
  if (Array.isArray(props)) {
    // 使用字符串数组的情况
    i = props.length
    // 使用while循环遍历该字符串数组
    while (i--) {
      val = props[i]
      if (typeof val === 'string') {
        // props数组中的元素为字符串的情况
        // camelize方法位于 src/shared/util.js 文件中,用于将中横线转为驼峰
        name = camelize(val)
        res[name] = { type: null }
      } else if (process.env.NODE_ENV !== 'production') {
        // props数组中的元素不为字符串的情况,在非生产环境下给予警告
        // warn方法位于 src/core/util/debug.js 文件中
        warn('props must be strings when using array syntax.')
      }
    }
  } else if (isPlainObject(props)) {
    // 使用对象的情况(注)
    // isPlainObject方法位于 src/shared/util.js 文件中,用于判断是否为普通对象
    for (const key in props) {
      val = props[key]
      name = camelize(key)
      // 使用for in循环对props每一个键的值进行判断,如果是普通对象就直接使用,否则将其作为type的值
      res[name] = isPlainObject(val)
        ? val
        : { type: val }
    }
  } else if (process.env.NODE_ENV !== 'production') {
    // 使用了props选项,但它的值既不是字符串数组,又不是对象的情况
    // toRawType方法位于 src/shared/util.js 文件中,用于判断真实的数据类型
    warn(
      `Invalid value for option "props": expected an Array or an Object, ` +
      `but got ${toRawType(props)}.`,
      vm
    )
  }
  options.props = res
}

如此一来,假如我的 props 是一个字符串数组:

props: ["team"]

经过这个函数之后,props 将被规范为:

props: {
  team:{
    type: null
  }
}

假如我的 props 是一个对象:

props: {
  name: String,
  height: {
    type: Number,
    default: 198
  }
}

经过这个函数之后,将被规范化为:

props: {
  name: {
    type: String
  },
  height: {
    type: Number,
    default: 198
  }
}

注:对象的写法也分为以下两种,故仍需进行规范化

props: {
  // 第一种写法,直接写类型
  name: String,
  // 第二种写法,写对象
  name: {
    type: String,
    default: 'Kobe Bryant'
  }
}

最终会被规范为第二种写法。

相关文章

  • Vue内部怎样处理props选项的多种写法

    开发过程中,props 的使用有两种写法: Vue在内部会对 props 选项进行处理,无论开发时使用了哪种语法,...

  • 配置项多种写法的实现

    Vue 是如何实现配置选项多种写法的,如 props inject 等选项既支持数组 + 字符串写法也支持对象写法...

  • Vue3 踩坑记,持续更新

    props的.sync在vue3中被移除 vue2中写法:组件内部: 定义 props:{name: Strin...

  • Vue props用法详解

    Vue props用法详解 组件接受的选项之一 props 是 Vue 中非常重要的一个选项。父子组件的关系可以总...

  • Vue props用法小结

    Vue props用法详解组件接受的选项之一 props 是 Vue 中非常重要的一个选项。父子组件的关系可以总结...

  • Vue 构造选项

    Vue 构造选项 Options 数据: data、props、methods、computed、watch DO...

  • 2018-06-14

    vue项目中 props想返回一个对象的时候 es5的写法: props:{ testObj:{ type...

  • Vue&React在Props上的处理

    声明: 1.在Props的传入方式上 Vue: Vue的Props分为两种第一种是在组件内部用props定义过的属...

  • 2022-03-02

    vue 3 绑定props的值类型如string| [] |boolean不定 ts如何处理报错 如 props的...

  • vue中props的默认写法

    注意:默认值类型为数组或对象,一定要在函数中返回这个默认值,而不是直接写

网友评论

    本文标题:Vue内部怎样处理props选项的多种写法

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