Skip to content

Commit f5fc087

Browse files
meixgallen-zh
authored andcommitted
fix(core & build): $refs 不生效 (#153)
1 parent fca7324 commit f5fc087

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed

packages/mars-build/src/compiler/template/render.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ function genData(el, state) {
195195

196196
// TODO: better genProps, omit static attrs
197197
// attributes
198-
if (attrs.length > 0) {
198+
if (attrs.length > 0 || el.ref) {
199199
const props = genProps(attrs, state, el);
200200
data += el.isComp ? `[_pp(${props})],` : `[${props}],`;
201201
}
@@ -386,6 +386,10 @@ function genProps(props, state, el) {
386386
if (fData.length) {
387387
res += `_p:${state.processFilterData(fData, 'p', el)},`;
388388
}
389+
390+
if (el.ref) {
391+
res += `'ref': ${el.ref},`;
392+
}
389393
return res.slice(0, -1) + '}';
390394
}
391395

packages/mars-core/src/base/createComponent.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export function makeCreateComponent(handleProxy, handleModel, setData, callHook)
7676
let props = normalizeProps(options.props);
7777
props = Object.assign(props, {
7878
compId: String,
79+
ref: String,
7980
rootComputed: Object,
8081
rootUID: Number
8182
});

packages/mars-core/src/base/mixins.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import {getPageInstance} from '../helper/instance';
7-
import {setObjectData} from '../helper/util';
7+
import {setObjectData, remove} from '../helper/util';
88
import config from '../config';
99

1010
export function makePageMixin($api) {
@@ -33,7 +33,8 @@ export function makeGetCompMixin($api) {
3333
return function getCompMixin(options) {
3434
return {
3535
props: {
36-
compId: String
36+
compId: String,
37+
ref: String
3738
},
3839
beforeCreate() {
3940
this.$api = $api;
@@ -54,6 +55,20 @@ export function makeGetCompMixin($api) {
5455

5556
// 此时还没有 .$mp
5657
this.$options.mpInstance.__curSwan__ = curIndex;
58+
59+
registerRef(this, this.ref);
60+
},
61+
destroyed() {
62+
registerRef(this, this.ref, true);
63+
},
64+
watch: {
65+
ref(newVal, val) {
66+
if (val !== newVal) {
67+
registerRef(this, val, true);
68+
registerRef(this, newVal);
69+
return;
70+
}
71+
}
5772
},
5873
updated() {
5974
this.$emit('vm.updated');
@@ -142,3 +157,34 @@ export function handleModel(event) {
142157

143158

144159
}
160+
161+
function registerRef(vm, key, isRemoval) {
162+
if (!key) {
163+
return;
164+
}
165+
166+
const refs = vm.$parent.$refs;
167+
const ref = vm;
168+
169+
if (isRemoval) {
170+
if (Array.isArray(refs[key])) {
171+
remove(refs[key], ref);
172+
}
173+
else if (refs[key] === ref) {
174+
refs[key] = undefined;
175+
}
176+
}
177+
else {
178+
if (vm.compId && vm.compId.split(',').pop().indexOf('-') !== -1) {
179+
if (!Array.isArray(refs[key])) {
180+
refs[key] = [ref];
181+
}
182+
else if (refs[key].indexOf(ref) < 0) {
183+
refs[key].push(ref);
184+
}
185+
}
186+
else {
187+
refs[key] = ref;
188+
}
189+
}
190+
}

packages/mars-core/src/base/vue/vue.runtime.esm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ var isBuiltInTag = makeMap('slot,component', true);
104104
/**
105105
* Check if an attribute is a reserved attribute.
106106
*/
107-
var isReservedAttribute = makeMap('key,ref,slot,slot-scope,is');
107+
var isReservedAttribute = makeMap('key,slot,slot-scope,is');
108108

109109
/**
110110
* Remove an item from an array.

packages/mars-core/src/helper/util.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,15 @@ export function setObjectData(target, model, value) {
4949
}
5050
}
5151
}
52+
53+
/**
54+
* Remove an item from an array.
55+
*/
56+
export function remove(arr, item) {
57+
if (arr.length) {
58+
const index = arr.indexOf(item);
59+
if (index > -1) {
60+
return arr.splice(index, 1);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)