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