提交 8aff183a 编写于 作者: fxy060608's avatar fxy060608

feat(mp): $refs

上级 30362f36
......@@ -258,7 +258,12 @@ function hasHook(name) {
return false;
}
function callHook(name, args) {
if (name === 'onLoad' && args && args.__id__) {
if (name === 'mounted') {
callHook.call(this, 'bm'); // beforeMount
this.$.isMounted = true;
name = 'm';
}
else if (name === 'onLoad' && args && args.__id__) {
this.__eventChannel__ = getEventChannel(args.__id__);
delete args.__id__;
}
......
......@@ -248,7 +248,12 @@ function hasHook(name) {
return false;
}
function callHook(name, args) {
if (name === 'onLoad' && args && args.__id__) {
if (name === 'mounted') {
callHook.call(this, 'bm'); // beforeMount
this.$.isMounted = true;
name = 'm';
}
else if (name === 'onLoad' && args && args.__id__) {
this.__eventChannel__ = getEventChannel(args.__id__);
delete args.__id__;
}
......@@ -386,15 +391,18 @@ function initWxsCallMethods(methods, wxsCallMethods) {
};
});
}
function selectAllComponents(mpInstance, selector, $refs) {
const components = mpInstance.selectAllComponents(selector);
components.forEach((component) => {
const ref = component.dataset.ref;
$refs[ref] = component.$vm || component;
});
}
function initRefs(instance, mpInstance) {
Object.defineProperty(instance, 'refs', {
get() {
const $refs = {};
const components = mpInstance.selectAllComponents('.vue-ref');
components.forEach((component) => {
const ref = component.dataset.ref;
$refs[ref] = component.$vm || component;
});
selectAllComponents(mpInstance, '.vue-ref', $refs);
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
forComponents.forEach((component) => {
const ref = component.dataset.ref;
......
......@@ -211,7 +211,11 @@ function hasHook(this: ComponentPublicInstance, name: string) {
}
function callHook(this: ComponentPublicInstance, name: string, args?: unknown) {
if (name === 'onLoad' && args && (args as any).__id__) {
if (name === 'mounted') {
callHook.call(this, 'bm') // beforeMount
this.$.isMounted = true
name = 'm'
} else if (name === 'onLoad' && args && (args as any).__id__) {
;(this as any).__eventChannel__ = getEventChannel((args as any).__id__)
delete (args as any).__id__
}
......
......@@ -56,6 +56,31 @@ export function initWxsCallMethods(
})
}
function selectAllComponents(
mpInstance: MPComponentInstance,
selector: string,
$refs: Record<string, ComponentPublicInstance>
) {
const components = mpInstance.selectAllComponents(selector)
components.forEach((component) => {
const ref = component.dataset.ref
$refs[ref] = component.$vm || component
if (__PLATFORM__ === 'mp-weixin') {
if (component.dataset.vueGeneric === 'scoped') {
component
.selectAllComponents('.scoped-ref')
.forEach((scopedComponent) => {
selectAllComponents(
scopedComponent as MPComponentInstance,
selector,
$refs
)
})
}
}
})
}
export function initRefs(
instance: ComponentInternalInstance,
mpInstance: MPComponentInstance
......@@ -63,11 +88,7 @@ export function initRefs(
Object.defineProperty(instance, 'refs', {
get() {
const $refs: Record<string, any> = {}
const components = mpInstance.selectAllComponents('.vue-ref')
components.forEach((component) => {
const ref = component.dataset.ref
$refs[ref] = component.$vm || component
})
selectAllComponents(mpInstance, '.vue-ref', $refs)
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for')
forComponents.forEach((component) => {
const ref = component.dataset.ref
......
......@@ -248,7 +248,12 @@ function hasHook(name) {
return false;
}
function callHook(name, args) {
if (name === 'onLoad' && args && args.__id__) {
if (name === 'mounted') {
callHook.call(this, 'bm'); // beforeMount
this.$.isMounted = true;
name = 'm';
}
else if (name === 'onLoad' && args && args.__id__) {
this.__eventChannel__ = getEventChannel(args.__id__);
delete args.__id__;
}
......@@ -386,15 +391,18 @@ function initWxsCallMethods(methods, wxsCallMethods) {
};
});
}
function selectAllComponents(mpInstance, selector, $refs) {
const components = mpInstance.selectAllComponents(selector);
components.forEach((component) => {
const ref = component.dataset.ref;
$refs[ref] = component.$vm || component;
});
}
function initRefs(instance, mpInstance) {
Object.defineProperty(instance, 'refs', {
get() {
const $refs = {};
const components = mpInstance.selectAllComponents('.vue-ref');
components.forEach((component) => {
const ref = component.dataset.ref;
$refs[ref] = component.$vm || component;
});
selectAllComponents(mpInstance, '.vue-ref', $refs);
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
forComponents.forEach((component) => {
const ref = component.dataset.ref;
......
......@@ -248,7 +248,12 @@ function hasHook(name) {
return false;
}
function callHook(name, args) {
if (name === 'onLoad' && args && args.__id__) {
if (name === 'mounted') {
callHook.call(this, 'bm'); // beforeMount
this.$.isMounted = true;
name = 'm';
}
else if (name === 'onLoad' && args && args.__id__) {
this.__eventChannel__ = getEventChannel(args.__id__);
delete args.__id__;
}
......@@ -389,15 +394,18 @@ function initWxsCallMethods(methods, wxsCallMethods) {
};
});
}
function selectAllComponents(mpInstance, selector, $refs) {
const components = mpInstance.selectAllComponents(selector);
components.forEach((component) => {
const ref = component.dataset.ref;
$refs[ref] = component.$vm || component;
});
}
function initRefs(instance, mpInstance) {
Object.defineProperty(instance, 'refs', {
get() {
const $refs = {};
const components = mpInstance.selectAllComponents('.vue-ref');
components.forEach((component) => {
const ref = component.dataset.ref;
$refs[ref] = component.$vm || component;
});
selectAllComponents(mpInstance, '.vue-ref', $refs);
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
forComponents.forEach((component) => {
const ref = component.dataset.ref;
......
import { isSymbol, extend, isMap, isObject, toRawType, def, isArray, isString, isFunction, isPromise, toHandlerKey, remove, EMPTY_OBJ, camelize, capitalize, normalizeClass, normalizeStyle, isOn, NOOP, isGloballyWhitelisted, isIntegerKey, hasOwn, hasChanged, invokeArrayFns as invokeArrayFns$1, makeMap, isSet, NO, toNumber, hyphenate, isReservedProp, EMPTY_ARR, toTypeString } from '@vue/shared';
import { isSymbol, extend, isMap, isObject, toRawType, def, isArray, isString, isFunction, isPromise, toHandlerKey, remove, EMPTY_OBJ, camelize, capitalize, normalizeClass, normalizeStyle, isOn, NOOP, isGloballyWhitelisted, isIntegerKey, hasOwn, hasChanged, invokeArrayFns, makeMap, isSet, NO, toNumber, hyphenate, isReservedProp, EMPTY_ARR, toTypeString } from '@vue/shared';
export { camelize } from '@vue/shared';
const invokeArrayFns = (fns, arg) => {
let ret;
for (let i = 0; i < fns.length; i++) {
ret = fns[i](arg);
}
return ret;
};
const ON_ERROR = 'onError';
const targetMap = new WeakMap();
......@@ -3055,29 +3048,8 @@ const publicPropertiesMap = extend(Object.create(null), {
$options: i => (__VUE_OPTIONS_API__ ? resolveMergedOptions(i) : i.type),
$forceUpdate: i => () => queueJob(i.update),
// $nextTick: i => nextTick.bind(i.proxy!), // fixed by xxxxxx
$watch: i => (__VUE_OPTIONS_API__ ? instanceWatch.bind(i) : NOOP),
// compat
$children: getCompatChildren
$watch: i => (__VUE_OPTIONS_API__ ? instanceWatch.bind(i) : NOOP)
});
function getCompatChildren(instance) {
const root = instance.subTree;
const children = [];
if (root) {
walk(root, children);
}
return children;
}
function walk(vnode, children) {
if (vnode.component) {
children.push(vnode.component.proxy);
}
else if (vnode.shapeFlag & 16 /* ARRAY_CHILDREN */) {
const vnodes = vnode.children;
for (let i = 0; i < vnodes.length; i++) {
walk(vnodes[i], children);
}
}
}
const PublicInstanceProxyHandlers = {
get({ _: instance }, key) {
const { ctx, setupState, data, props, accessCache, type, appContext } = instance;
......@@ -3981,8 +3953,8 @@ const prodEffectOptions = {
function createDevEffectOptions(instance) {
return {
scheduler: queueJob,
onTrack: instance.rtc ? e => invokeArrayFns$1(instance.rtc, e) : void 0,
onTrigger: instance.rtg ? e => invokeArrayFns$1(instance.rtg, e) : void 0
onTrack: instance.rtc ? e => invokeArrayFns(instance.rtc, e) : void 0,
onTrigger: instance.rtg ? e => invokeArrayFns(instance.rtg, e) : void 0
};
}
function setupRenderEffect(instance) {
......@@ -3998,7 +3970,7 @@ function setupRenderEffect(instance) {
const { bu, u } = instance;
// beforeUpdate hook
if (bu) {
invokeArrayFns$1(bu);
invokeArrayFns(bu);
}
patch(instance);
// updated hook
......@@ -4012,7 +3984,7 @@ function unmountComponent(instance) {
const { bum, effects, update, um } = instance;
// beforeUnmount hook
if (bum) {
invokeArrayFns$1(bum);
invokeArrayFns(bum);
}
if (effects) {
for (let i = 0; i < effects.length; i++) {
......@@ -4090,17 +4062,6 @@ function applyOptions(options, instance, publicThis) {
function set(target, key, val) {
return (target[key] = val);
}
function hasHook(name) {
const hooks = this.$[name];
if (hooks && hooks.length) {
return true;
}
return false;
}
function callHook(name, args) {
const hooks = this.$[name];
return hooks && invokeArrayFns(hooks, args);
}
function errorHandler(err, instance, info) {
......@@ -4169,11 +4130,6 @@ function initApp(app) {
}
const globalProperties = appConfig.globalProperties;
uniIdMixin(globalProperties);
{
// 小程序,待重构,不再挂靠全局
globalProperties.$hasHook = hasHook;
globalProperties.$callHook = callHook;
}
if (__VUE_OPTIONS_API__) {
globalProperties.$set = set;
globalProperties.$applyOptions = applyOptions;
......@@ -4183,16 +4139,6 @@ function initApp(app) {
var plugin = {
install(app) {
initApp(app);
const globalProperties = app._context.config.globalProperties;
const oldCallHook = globalProperties.$callHook;
globalProperties.$callHook = function callHook(name, args) {
if (name === 'mounted') {
oldCallHook.call(this, 'bm'); // beforeMount
this.$.isMounted = true;
name = 'm';
}
return oldCallHook.call(this, name, args);
};
const oldMount = app.mount;
app.mount = function mount(rootContainer) {
const instance = oldMount.call(app, rootContainer);
......
......@@ -3046,29 +3046,8 @@ const publicPropertiesMap = extend(Object.create(null), {
$options: i => (__VUE_OPTIONS_API__ ? resolveMergedOptions(i) : i.type),
$forceUpdate: i => () => queueJob(i.update),
// $nextTick: i => nextTick.bind(i.proxy!), // fixed by xxxxxx
$watch: i => (__VUE_OPTIONS_API__ ? instanceWatch.bind(i) : NOOP),
// compat
$children: getCompatChildren
$watch: i => (__VUE_OPTIONS_API__ ? instanceWatch.bind(i) : NOOP)
});
function getCompatChildren(instance) {
const root = instance.subTree;
const children = [];
if (root) {
walk(root, children);
}
return children;
}
function walk(vnode, children) {
if (vnode.component) {
children.push(vnode.component.proxy);
}
else if (vnode.shapeFlag & 16 /* ARRAY_CHILDREN */) {
const vnodes = vnode.children;
for (let i = 0; i < vnodes.length; i++) {
walk(vnodes[i], children);
}
}
}
const PublicInstanceProxyHandlers = {
get({ _: instance }, key) {
const { ctx, setupState, data, props, accessCache, type, appContext } = instance;
......
......@@ -5,20 +5,6 @@ import { initApp } from '@dcloudio/uni-vue'
export default {
install(app: App) {
initApp(app)
const globalProperties = app._context.config.globalProperties
const oldCallHook = globalProperties.$callHook
globalProperties.$callHook = function callHook(
name: string,
args?: unknown
) {
if (name === 'mounted') {
oldCallHook.call(this, 'bm') // beforeMount
this.$.isMounted = true
name = 'm'
}
return oldCallHook.call(this, name, args)
}
const oldMount = app.mount
app.mount = function mount(rootContainer: any) {
const instance = oldMount.call(app, rootContainer)
......
......@@ -185,7 +185,12 @@ function hasHook(name) {
return false;
}
function callHook(name, args) {
if (name === 'onLoad' && args && args.__id__) {
if (name === 'mounted') {
callHook.call(this, 'bm'); // beforeMount
this.$.isMounted = true;
name = 'm';
}
else if (name === 'onLoad' && args && args.__id__) {
this.__eventChannel__ = getEventChannel(args.__id__);
delete args.__id__;
}
......@@ -323,15 +328,27 @@ function initWxsCallMethods(methods, wxsCallMethods) {
};
});
}
function selectAllComponents(mpInstance, selector, $refs) {
const components = mpInstance.selectAllComponents(selector);
components.forEach((component) => {
const ref = component.dataset.ref;
$refs[ref] = component.$vm || component;
{
if (component.dataset.vueGeneric === 'scoped') {
component
.selectAllComponents('.scoped-ref')
.forEach((scopedComponent) => {
selectAllComponents(scopedComponent, selector, $refs);
});
}
}
});
}
function initRefs(instance, mpInstance) {
Object.defineProperty(instance, 'refs', {
get() {
const $refs = {};
const components = mpInstance.selectAllComponents('.vue-ref');
components.forEach((component) => {
const ref = component.dataset.ref;
$refs[ref] = component.$vm || component;
});
selectAllComponents(mpInstance, '.vue-ref', $refs);
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
forComponents.forEach((component) => {
const ref = component.dataset.ref;
......
......@@ -248,7 +248,12 @@ function hasHook(name) {
return false;
}
function callHook(name, args) {
if (name === 'onLoad' && args && args.__id__) {
if (name === 'mounted') {
callHook.call(this, 'bm'); // beforeMount
this.$.isMounted = true;
name = 'm';
}
else if (name === 'onLoad' && args && args.__id__) {
this.__eventChannel__ = getEventChannel(args.__id__);
delete args.__id__;
}
......@@ -386,15 +391,18 @@ function initWxsCallMethods(methods, wxsCallMethods) {
};
});
}
function selectAllComponents(mpInstance, selector, $refs) {
const components = mpInstance.selectAllComponents(selector);
components.forEach((component) => {
const ref = component.dataset.ref;
$refs[ref] = component.$vm || component;
});
}
function initRefs(instance, mpInstance) {
Object.defineProperty(instance, 'refs', {
get() {
const $refs = {};
const components = mpInstance.selectAllComponents('.vue-ref');
components.forEach((component) => {
const ref = component.dataset.ref;
$refs[ref] = component.$vm || component;
});
selectAllComponents(mpInstance, '.vue-ref', $refs);
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
forComponents.forEach((component) => {
const ref = component.dataset.ref;
......
......@@ -106,10 +106,10 @@ export declare const defaultRpx2Unit: {
declare type DictArray = [number, number][];
export declare class EventChannel {
id: number;
id?: number;
private listener;
private emitCache;
constructor(id: number, events?: NavigateToOptionEvents);
constructor(id?: number, events?: NavigateToOptionEvents);
emit(eventName: string, ...args: any[]): number | undefined;
on(eventName: string, fn: EventChannelListener['fn']): void;
once(eventName: string, fn: EventChannelListener['fn']): void;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册