MCPcopy Index your code
hub / github.com/ygs-code/vue / assertProp

Function assertProp

vue.js:2406–2461  ·  view source on GitHub ↗

* Assert whether a prop is valid. * 断言一个属性是否有效。 * * * * prop, //属性的type值 key, //props属性中的key value, //view 属性的值 vm, //组件构造函数 absent //false

(
        prop,  //属性的type值
        name, //props属性中的key
        value, //view 属性的值
        vm, //组件构造函数
        absent//false
    )

Source from the content-addressed store, hash-verified

2404
2405
2406 function assertProp(
2407 prop, //属性的type值
2408 name, //props属性中的key
2409 value, //view 属性的值
2410 vm, //组件构造函数
2411 absent//false
2412 ) {
2413 //必须有required 和 absent
2414 if (prop.required && absent) {
2415 warn(
2416 'Missing required prop: "' + name + '"',
2417 vm
2418 );
2419 return
2420 }
2421 //如果vual 为空 或者 不是必填项 则不执行下面代码
2422 if (value == null && !prop.required) {
2423 return
2424 }
2425 //类型
2426 var type = prop.type;
2427
2428 //如果类型为真 或者类型 不存在
2429 var valid = !type || type === true;
2430
2431 var expectedTypes = [];
2432
2433 if (type) { //如果type存在
2434 if (!Array.isArray(type)) { //如果不是数组
2435 type = [type]; //再包裹成数组
2436 }
2437 for (var i = 0; i < type.length && !valid; i++) {
2438 var assertedType = assertType(value, type[i]);
2439 expectedTypes.push(assertedType.expectedType || '');
2440 valid = assertedType.valid;
2441 }
2442 }
2443 if (!valid) {
2444 warn(
2445 "Invalid prop: type check failed for prop \"" + name + "\"." +
2446 " Expected " + (expectedTypes.map(capitalize).join(', ')) +
2447 ", got " + (toRawType(value)) + ".",
2448 vm
2449 );
2450 return
2451 }
2452 var validator = prop.validator;
2453 if (validator) {
2454 if (!validator(value)) {
2455 warn(
2456 'Invalid prop: custom validator check failed for prop "' + name + '".',
2457 vm
2458 );
2459 }
2460 }
2461 }
2462
2463 //检测数据类型 是否是String|Number|Boolean|Function|Symbol 其中的一个数据类型

Callers 1

validatePropFunction · 0.85

Calls 2

assertTypeFunction · 0.85
toRawTypeFunction · 0.85

Tested by

no test coverage detected