From 2ba8c12afcb7a78f225d7cd9bdd964fbdcf28547 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Thu, 19 Sep 2019 21:20:28 +0800 Subject: [PATCH] fix(mp-toutiao): improve $refs #791 --- .../mp-toutiao/runtime/wrapper/util.js | 51 ++++++++++++++----- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/src/platforms/mp-toutiao/runtime/wrapper/util.js b/src/platforms/mp-toutiao/runtime/wrapper/util.js index eee1e2695d..9f3a58a218 100644 --- a/src/platforms/mp-toutiao/runtime/wrapper/util.js +++ b/src/platforms/mp-toutiao/runtime/wrapper/util.js @@ -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) -- GitLab