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

作者:边城少年_ 时间:2024-05-03 15:10:39 

开发过程中,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'
}
}

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

来源:https://www.jianshu.com/p/f528880d9051

标签:Vue,props,选项
0
投稿

猜你喜欢

  • Python操作JSON实现网络数据交换

    2023-01-27 02:21:53
  • 如何使用Python 抓取和优化所有网站图像

    2022-05-18 12:36:00
  • python清除函数占用的内存方法

    2021-10-29 19:15:47
  • 关于django连接mysql数据库并进行数据库的创建的问题

    2024-01-22 04:50:12
  • Python切片操作实例分析

    2022-05-02 17:49:25
  • js实现登录验证码

    2023-09-06 05:43:27
  • python在前端页面使用 MySQLdb 连接数据

    2024-01-21 07:30:09
  • python中的三种注释方法

    2023-05-27 07:47:01
  • IE7的web标准之道 Ⅲ

    2008-08-20 12:55:00
  • Java中@Pattern注解常用的校验正则表达式学习笔记

    2022-08-07 11:12:10
  • python中引用与复制用法实例分析

    2022-09-04 09:54:35
  • Python常见字典内建函数用法示例

    2022-08-07 08:59:23
  • ORACLE 如何查询被锁定表及如何解锁释放session

    2023-07-02 11:59:39
  • mysql 5.7.17 winx64安装配置方法图文教程

    2024-01-25 08:23:38
  • OpenCV形状检测的示例详解

    2023-11-11 06:17:14
  • 如何使用Python调整图像大小

    2021-11-30 00:10:14
  • TensorFlow人工智能学习创建数据实现示例详解

    2022-04-20 12:58:27
  • 在数据库‘master’中拒绝CREATE DATABASE权限问题的解决方法

    2011-10-24 19:46:55
  • Python图像处理库PIL的ImageEnhance模块使用介绍

    2023-05-14 17:29:51
  • PyQt5组件读取参数的实例

    2023-04-02 07:47:29
  • asp之家 网络编程 m.aspxhome.com