Skip to content

Commit 29cee1a

Browse files
authored
Merge pull request #508 from Meituan-Dianping/develop
Develop
2 parents e8ae625 + ee07244 commit 29cee1a

File tree

16 files changed

+111
-16
lines changed

16 files changed

+111
-16
lines changed

dist/vue.common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4629,7 +4629,7 @@ Object.defineProperty(Vue$3.prototype, '$ssrContext', {
46294629
});
46304630

46314631
Vue$3.version = '2.4.1';
4632-
Vue$3.mpvueVersion = '1.0.8';
4632+
Vue$3.mpvueVersion = '1.0.11';
46334633

46344634
/* */
46354635

dist/vue.esm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4627,7 +4627,7 @@ Object.defineProperty(Vue$3.prototype, '$ssrContext', {
46274627
});
46284628

46294629
Vue$3.version = '2.4.1';
4630-
Vue$3.mpvueVersion = '1.0.8';
4630+
Vue$3.mpvueVersion = '1.0.11';
46314631

46324632
/* */
46334633

dist/vue.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4618,7 +4618,7 @@ Object.defineProperty(Vue$3.prototype, '$ssrContext', {
46184618
});
46194619

46204620
Vue$3.version = '2.4.1';
4621-
Vue$3.mpvueVersion = '1.0.8';
4621+
Vue$3.mpvueVersion = '1.0.11';
46224622

46234623
/* */
46244624

dist/vue.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vue.runtime.common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4625,7 +4625,7 @@ Object.defineProperty(Vue$3.prototype, '$ssrContext', {
46254625
});
46264626

46274627
Vue$3.version = '2.4.1';
4628-
Vue$3.mpvueVersion = '1.0.8';
4628+
Vue$3.mpvueVersion = '1.0.11';
46294629

46304630
/* */
46314631

dist/vue.runtime.esm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4623,7 +4623,7 @@ Object.defineProperty(Vue$3.prototype, '$ssrContext', {
46234623
});
46244624

46254625
Vue$3.version = '2.4.1';
4626-
Vue$3.mpvueVersion = '1.0.8';
4626+
Vue$3.mpvueVersion = '1.0.11';
46274627

46284628
/* */
46294629

dist/vue.runtime.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4614,7 +4614,7 @@ Object.defineProperty(Vue$3.prototype, '$ssrContext', {
46144614
});
46154615

46164616
Vue$3.version = '2.4.1';
4617-
Vue$3.mpvueVersion = '1.0.8';
4617+
Vue$3.mpvueVersion = '1.0.11';
46184618

46194619
/* */
46204620

dist/vue.runtime.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/mpvue-template-compiler/build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4580,7 +4580,7 @@ var component = {
45804580
var mpcomid = ast.mpcomid;
45814581
var slots = ast.slots;
45824582
if (slotName) {
4583-
attrsMap['data'] = "{{...$root[$p], $root}}";
4583+
attrsMap['data'] = "{{...$root[$k], $root}}";
45844584
attrsMap['is'] = "{{" + slotName + "}}";
45854585
} else {
45864586
var slotsName = getSlotsName(slots);

packages/mpvue-template-compiler/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mpvue-template-compiler",
3-
"version": "1.0.11",
3+
"version": "1.0.12",
44
"description": "mpvue template compiler for Vue",
55
"main": "index.js",
66
"repository": {

packages/mpvue/index.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4144,7 +4144,7 @@ Object.defineProperty(Vue$3.prototype, '$ssrContext', {
41444144
});
41454145

41464146
Vue$3.version = '2.4.1';
4147-
Vue$3.mpvueVersion = '1.0.9';
4147+
Vue$3.mpvueVersion = '1.0.11';
41484148

41494149
/* globals renderer */
41504150

@@ -4974,6 +4974,48 @@ function getGlobalData (app, rootVueVM) {
49744974
}
49754975
}
49764976

4977+
/**
4978+
* 格式化 properties 属性,并给每个属性加上 observer 方法
4979+
*/
4980+
function normalizeProperties (vm) {
4981+
var properties = vm.$options.properties || {};
4982+
var res = {};
4983+
var val;
4984+
var loop = function ( key ) {
4985+
val = isPlainObject(properties[key])
4986+
? properties[key]
4987+
: { type: properties[key] };
4988+
res[key] = {
4989+
type: val.type,
4990+
value: val.value,
4991+
observer: function observer (newVal, oldVal) {
4992+
vm[key] = newVal; // 先修改值再触发原始的 observer,跟 watch 行为保持一致
4993+
if (typeof val.observer === 'function') {
4994+
val.observer.call(vm, newVal, oldVal);
4995+
}
4996+
}
4997+
};
4998+
};
4999+
5000+
for (var key in properties) loop( key );
5001+
return res
5002+
}
5003+
5004+
/**
5005+
* 把 properties 中的属性 proxy 到 vm 上
5006+
*/
5007+
function initMpProps (vm) {
5008+
var mpProps = vm._mpProps = {};
5009+
var keys = Object.keys(vm.$options.properties || {});
5010+
keys.forEach(function (key) {
5011+
if (!(key in vm)) {
5012+
proxy(vm, '_mpProps', key);
5013+
mpProps[key] = undefined; // for observe
5014+
}
5015+
});
5016+
observe(mpProps, true);
5017+
}
5018+
49775019
function initMP (mpType, next) {
49785020
var rootVueVM = this.$root;
49795021
if (!rootVueVM.$mp) {
@@ -5041,7 +5083,11 @@ function initMP (mpType, next) {
50415083
}
50425084
});
50435085
} else if (mpType === 'component') {
5086+
initMpProps(rootVueVM);
5087+
50445088
global.Component({
5089+
// 小程序原生的组件属性
5090+
properties: normalizeProperties(rootVueVM),
50455091
// 页面的初始数据
50465092
data: {
50475093
$root: {}
@@ -5192,6 +5238,7 @@ function getVmData (vm) {
51925238
var dataKeys = [].concat(
51935239
Object.keys(vm._data || {}),
51945240
Object.keys(vm._props || {}),
5241+
Object.keys(vm._mpProps || {}),
51955242
Object.keys(vm._computedWatchers || {})
51965243
);
51975244
return dataKeys.reduce(function (res, key) {

packages/mpvue/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mpvue",
3-
"version": "1.0.11",
3+
"version": "1.0.12",
44
"description": "Vue Runtime for mini program",
55
"main": "index.js",
66
"repository": {

src/platforms/mp/compiler/codegen/convert/component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default {
1414
convertComponent (ast, components, slotName) {
1515
const { attrsMap, tag, mpcomid, slots } = ast
1616
if (slotName) {
17-
attrsMap['data'] = `{{...$root[$p], $root}}`
17+
attrsMap['data'] = `{{...$root[$k], $root}}`
1818
attrsMap['is'] = `{{${slotName}}}`
1919
} else {
2020
const slotsName = getSlotsName(slots)

src/platforms/mp/runtime/lifecycle.js

Lines changed: 48 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,46 @@ function getGlobalData (app, rootVueVM) {
3740
}
3841
}
3942

43+
/**
44+
* 格式化 properties 属性,并给每个属性加上 observer 方法
45+
*/
46+
function normalizeProperties (vm) {
47+
const properties = vm.$options.properties || {}
48+
const res = {}
49+
let val
50+
for (const key in properties) {
51+
val = isPlainObject(properties[key])
52+
? properties[key]
53+
: { type: properties[key] }
54+
res[key] = {
55+
type: val.type,
56+
value: val.value,
57+
observer (newVal, oldVal) {
58+
vm[key] = newVal // 先修改值再触发原始的 observer,跟 watch 行为保持一致
59+
if (typeof val.observer === 'function') {
60+
val.observer.call(vm, newVal, oldVal)
61+
}
62+
}
63+
}
64+
}
65+
return res
66+
}
67+
68+
/**
69+
* 把 properties 中的属性 proxy 到 vm 上
70+
*/
71+
function initMpProps (vm) {
72+
const mpProps = vm._mpProps = {}
73+
const keys = Object.keys(vm.$options.properties || {})
74+
keys.forEach(key => {
75+
if (!(key in vm)) {
76+
proxy(vm, '_mpProps', key)
77+
mpProps[key] = undefined // for observe
78+
}
79+
})
80+
observe(mpProps, true)
81+
}
82+
4083
export function initMP (mpType, next) {
4184
const rootVueVM = this.$root
4285
if (!rootVueVM.$mp) {
@@ -100,7 +143,11 @@ export function initMP (mpType, next) {
100143
}
101144
})
102145
} else if (mpType === 'component') {
146+
initMpProps(rootVueVM)
147+
103148
global.Component({
149+
// 小程序原生的组件属性
150+
properties: normalizeProperties(rootVueVM),
104151
// 页面的初始数据
105152
data: {
106153
$root: {}

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) => {

test/mp/compiler/index.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ describe('slot', () => {
528528
it('插槽', () => {
529529
assertCodegen(
530530
`<div><slot>test</slot></div>`,
531-
`<template name="a"><view class="_div testModuleId"><template name="default">test</template><template data="{{...$root[$p], $root}}" is="{{$slotdefault || 'default'}}"></template></view></template>`,
531+
`<template name="a"><view class="_div testModuleId"><template name="default">test</template><template data="{{...$root[$k], $root}}" is="{{$slotdefault || 'default'}}"></template></view></template>`,
532532
{
533533
name: 'a',
534534
moduleId: 'testModuleId'
@@ -539,7 +539,7 @@ describe('slot', () => {
539539
it('使用', () => {
540540
assertCodegen(
541541
`<div><slot name="w">test</slot></div>`,
542-
`<template name="a"><view class="_div"><template name="w">test</template><template data="{{...$root[$p], $root}}" is="{{$slotw || 'w'}}"></template></view></template>`,
542+
`<template name="a"><view class="_div"><template name="w">test</template><template data="{{...$root[$k], $root}}" is="{{$slotw || 'w'}}"></template></view></template>`,
543543
{
544544
name: 'a'
545545
}

0 commit comments

Comments
 (0)