Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 95a9bac

Browse files
authored
Merge pull request Meituan-Dianping#490 from jkzing/native-component
feat: support miniprogram native component properties
2 parents 3ceffd8 + 2be260e commit 95a9bac

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/platforms/mp/runtime/lifecycle.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { handleError } from '../../../core/util/index'
1+
import { isPlainObject } from 'shared/util'
2+
import { handleError } from 'core/util/index'
3+
import { observe } from 'core/observer/index'
4+
import { proxy } from 'core/instance/state'
25

36
export function callHook (vm, hook, params) {
47
let handlers = vm.$options[hook]
@@ -37,6 +40,40 @@ function getGlobalData (app, rootVueVM) {
3740
}
3841
}
3942

43+
function normalizeProperties (vm) {
44+
const properties = vm.$options.properties
45+
const res = {}
46+
let val
47+
for (const key in properties) {
48+
val = isPlainObject(properties[key])
49+
? properties[key]
50+
: { type: properties[key] }
51+
res[key] = {
52+
type: val.type,
53+
value: val.value,
54+
observer (newVal, oldVal) {
55+
vm[key] = newVal
56+
if (typeof val.observer === 'function') {
57+
val.observer.call(vm, newVal, oldVal)
58+
}
59+
}
60+
}
61+
}
62+
return res
63+
}
64+
65+
function initMpProps (vm) {
66+
const mpProps = vm._mpProps = {}
67+
const keys = Object.keys(vm.$options.properties || {})
68+
keys.forEach(key => {
69+
if (!(key in vm)) {
70+
proxy(vm, '_mpProps', key)
71+
}
72+
mpProps[key] = undefined // for observe
73+
})
74+
observe(mpProps, true)
75+
}
76+
4077
export function initMP (mpType, next) {
4178
const rootVueVM = this.$root
4279
if (!rootVueVM.$mp) {
@@ -100,8 +137,11 @@ export function initMP (mpType, next) {
100137
}
101138
})
102139
} else if (mpType === 'component') {
140+
initMpProps(rootVueVM)
141+
103142
global.Component({
104143
// 页面的初始数据
144+
properties: normalizeProperties(rootVueVM),
105145
data: {
106146
$root: {}
107147
},

src/platforms/mp/runtime/render.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function getVmData (vm) {
2525
const dataKeys = [].concat(
2626
Object.keys(vm._data || {}),
2727
Object.keys(vm._props || {}),
28+
Object.keys(vm._mpProps || {}),
2829
Object.keys(vm._computedWatchers || {})
2930
)
3031
return dataKeys.reduce((res, key) => {

0 commit comments

Comments
 (0)