提交 2ba8c12a 编写于 作者: fxy060608's avatar fxy060608

fix(mp-toutiao): improve $refs #791

上级 a0fd8fe1
......@@ -8,23 +8,46 @@ export function isPage () {
return this.__nodeid__ === 0 || this.__nodeId__ === 0
}
export function initRefs (vm) {
export function initRefs (vm) {
const mpInstance = vm.$scope
mpInstance.selectAllComponents('.vue-ref', (components) => {
components.forEach(component => {
const ref = component.dataset.ref
vm.$refs[ref] = component.$vm || component
})
})
mpInstance.selectAllComponents('.vue-ref-in-for', (forComponents) => {
forComponents.forEach(component => {
const ref = component.dataset.ref
if (!vm.$refs[ref]) {
vm.$refs[ref] = []
const minorVersion = parseInt(tt.getSystemInfoSync().SDKVersion.split('.')[1])
if (minorVersion > 16) {
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 || 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 || component)
})
return $refs
}
vm.$refs[ref].push(component.$vm || component)
})
})
} else {
mpInstance.selectAllComponents('.vue-ref', (components) => {
components.forEach(component => {
const ref = component.dataset.ref
vm.$refs[ref] = component.$vm || component
})
})
mpInstance.selectAllComponents('.vue-ref-in-for', (forComponents) => {
forComponents.forEach(component => {
const ref = component.dataset.ref
if (!vm.$refs[ref]) {
vm.$refs[ref] = []
}
vm.$refs[ref].push(component.$vm || component)
})
})
}
}
const instances = Object.create(null)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册