Skip to content

Commit e10b8b0

Browse files
committed
add coerce option for component props
1 parent 9ce299b commit e10b8b0

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/directives/internal/prop.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import Watcher from '../../watcher'
77
import config from '../../config'
8-
import { assertProp, initProp } from '../../util/index'
8+
import { assertProp, initProp, coerceProp } from '../../util/index'
99

1010
const bindingModes = config._propBindingModes
1111

@@ -25,6 +25,7 @@ export default {
2525
parent,
2626
parentKey,
2727
function (val) {
28+
val = coerceProp(prop, val)
2829
if (assertProp(prop, val)) {
2930
child[childKey] = val
3031
}

src/util/component.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ function getIsBinding (el) {
7676

7777
export function initProp (vm, prop, value) {
7878
const key = prop.path
79+
value = coerceProp(prop, value)
7980
vm[key] = vm._data[key] = assertProp(prop, value)
8081
? value
8182
: undefined
@@ -143,6 +144,23 @@ export function assertProp (prop, value) {
143144
return true
144145
}
145146

147+
/**
148+
* Force parsing value with coerce option.
149+
*
150+
* @param {*} value
151+
* @param {Object} options
152+
* @return {*}
153+
*/
154+
155+
export function coerceProp (prop, value) {
156+
var coerce = prop.options.coerce
157+
if (!coerce) {
158+
return value
159+
}
160+
// coerce is a function
161+
return coerce(value)
162+
}
163+
146164
function formatType (val) {
147165
return val
148166
? val.charAt(0).toUpperCase() + val.slice(1)

0 commit comments

Comments
 (0)