diff --git a/packages/uni-mp-weixin/dist/mp.js b/packages/uni-mp-weixin/dist/mp.js index 393ef8c107bf380126216a9fb7ace5cad1383c93..a0536a20d6dca5780bc39e08ebcc904642a17da8 100644 --- a/packages/uni-mp-weixin/dist/mp.js +++ b/packages/uni-mp-weixin/dist/mp.js @@ -595,9 +595,7 @@ function validateProp (key, propsOptions, propsData, vm) { if (value !== undefined) { const propOptions = propsOptions[key]; const type = getType(propOptions); - if (type === Boolean) { - value = !!value; - } + value = formatVal(value, type); const observer = propOptions && propOptions.observer; if (observer) { // 初始化时,异步触发 observer,否则 observer 中无法访问 methods 或其他 @@ -610,6 +608,15 @@ function validateProp (key, propsOptions, propsData, vm) { return getPropertyVal(propsOptions[key]) } +function formatVal (val, type) { + if (type === Boolean) { + return !!val + } else if (type === String) { + return String(val) + } + return val +} + function observe (observer, vm, newVal, oldVal) { try { if (typeof observer === 'function') { @@ -665,12 +672,7 @@ function updateProperties (vm) { if (propsData && properties) { Object.keys(properties).forEach(key => { if (hasOwn(propsData, key)) { - const type = getType(properties[key]); - if (type === Boolean) { - vm[key] = !!propsData[key]; - } else { - vm[key] = propsData[key]; - } + vm[key] = formatVal(propsData[key], getType(properties[key])); } }); } diff --git a/src/core/runtime/mp/polyfill/state/properties.js b/src/core/runtime/mp/polyfill/state/properties.js index fa2b8e71a8f9982ee31110ccc0428b732c2ed1b5..0f361c1f5504b04fd203c92ff121769a02ae4069 100644 --- a/src/core/runtime/mp/polyfill/state/properties.js +++ b/src/core/runtime/mp/polyfill/state/properties.js @@ -39,9 +39,7 @@ function validateProp (key, propsOptions, propsData, vm) { if (value !== undefined) { const propOptions = propsOptions[key] const type = getType(propOptions) - if (type === Boolean) { - value = !!value - } + value = formatVal(value, type) const observer = propOptions && propOptions.observer if (observer) { // 初始化时,异步触发 observer,否则 observer 中无法访问 methods 或其他 @@ -54,6 +52,15 @@ function validateProp (key, propsOptions, propsData, vm) { return getPropertyVal(propsOptions[key]) } +function formatVal (val, type) { + if (type === Boolean) { + return !!val + } else if (type === String) { + return String(val) + } + return val +} + function observe (observer, vm, newVal, oldVal) { try { if (typeof observer === 'function') { @@ -109,12 +116,7 @@ export function updateProperties (vm) { if (propsData && properties) { Object.keys(properties).forEach(key => { if (hasOwn(propsData, key)) { - const type = getType(properties[key]) - if (type === Boolean) { - vm[key] = !!propsData[key] - } else { - vm[key] = propsData[key] - } + vm[key] = formatVal(propsData[key], getType(properties[key])) } }) }