New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
抖音 27.2.0 在 if 内的 ref 无法获取到 #4554
Comments
补充一个临时处理方案:
// src/plugins/refs-fixed.js
import Vue from 'vue';
/**
* @see {@link https://github.com/dcloudio/uni-app/issues/4554}
*/
export function install(vue = Vue) {
// #ifdef MP-TOUTIAO
vue.mixin({
created() {
initRefs(this);
},
});
// #endif
}
function initRefs(vm) {
const mpInstance = vm.$scope;
const refs = {};
Object.defineProperty(vm, '$refs', {
get() {
const $refs = {};
const components = mpInstance.selectAllComponents('.vue-ref') || [];
components.forEach((component) => {
const ref = component.dataset.ref;
$refs[ref] = component.$vm || toSkip(component);
});
const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for') || [];
forComponents.forEach((component) => {
const ref = component.dataset.ref;
if (!$refs[ref]) {
$refs[ref] = [];
}
$refs[ref].push(component.$vm || toSkip(component));
});
return syncRefs(refs, $refs);
},
});
}
function syncRefs(refs, newRefs) {
const oldKeys = new Set(...Object.keys(refs));
const newKeys = Object.keys(newRefs);
newKeys.forEach((key) => {
const oldValue = refs[key];
const newValue = newRefs[key];
if (
Array.isArray(oldValue) &&
Array.isArray(newValue) &&
oldValue.length === newValue.length &&
newValue.every((value) => oldValue.includes(value))
) {
return;
}
refs[key] = newValue;
oldKeys.delete(key);
});
oldKeys.forEach((key) => {
delete refs[key];
});
return refs;
}
function isObject(obj) {
return obj !== null && typeof obj === 'object';
}
function toSkip(obj) {
const OB = '__ob__';
const SKIP = '__v_skip';
if (isObject(obj) && Object.isExtensible(obj)) {
// 避免被 @vue/composition-api 观测
Object.defineProperty(obj, OB, {
configurable: true,
enumerable: false,
value: {
[SKIP]: true,
},
});
}
return obj;
}
// src/main.js
// ...
import { install as refsFixedInstall } from '@/plugins/refs-fixed';
refsFixedInstall();
// ... 注意:该方案在抖音 App 1.16.0 或以下版本不适用 |
感谢反馈,已复现问题。 |
临时解决方案:替换 文件 到目录 |
什么时候发布新版本? |
更新下HBuilderX,已经修复。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
问题描述
抖音 App 版本为 27.2.0 的抖音小程序,v-if 判断的 ref 组件,无法通过 this.$refs 拿到组件实例,如果是抖音App 27.1.0 版本的话,是正常的。
复现步骤
counter
组件预期结果
点击 + 按钮后,应该能看到数字 0 叠加到 1。
实际结果
报错,refs 拿不到 counterRef。
系统信息:
补充信息
经过排查,发现因为抖音App 27.2.0 版本的
SDKVersion
为3.0.0
,在initRefs
方法中,对selectAllComponents
API 做了一个兼容处理:但是只判断了
minorVersion > 16
,实际应该判断为majorVersion > 1 || minorVersion > 16
才对。手动修改代码如下图后,测试正常:
The text was updated successfully, but these errors were encountered: