提交 947050b6 编写于 作者: Q qiang

fix(mp): MPComponent skip deep observe

上级 b98183ff
...@@ -991,7 +991,8 @@ function observe (value, asRootData) { ...@@ -991,7 +991,8 @@ function observe (value, asRootData) {
!isServerRendering() && !isServerRendering() &&
(Array.isArray(value) || isPlainObject(value)) && (Array.isArray(value) || isPlainObject(value)) &&
Object.isExtensible(value) && Object.isExtensible(value) &&
!value._isVue !value._isVue &&
!value.__v_isMPComponent
) { ) {
ob = new Observer(value); ob = new Observer(value);
} }
...@@ -5600,7 +5601,7 @@ function nextTick$1(vm, cb) { ...@@ -5600,7 +5601,7 @@ function nextTick$1(vm, cb) {
function clearInstance(key, value) { function clearInstance(key, value) {
// 简易去除 Vue 和小程序组件实例 // 简易去除 Vue 和小程序组件实例
if (value) { if (value) {
if (value._isVue || (value.$vm && value.$vm._isVue)) { if (value._isVue || value.__v_isMPComponent) {
return {} return {}
} }
} }
......
import { handleProps } from './wrapper/util' import { handleProps, markMPComponent } from './wrapper/util'
const MPComponent = Component const MPComponent = Component
function initHook (name, options) { function initHook (name, options) {
const oldHook = options[name] const oldHook = options[name]
options[name] = function (...args) { options[name] = function (...args) {
markMPComponent(this)
const props = this.props const props = this.props
if (props && props['data-com-type'] === 'wx') { if (props && props['data-com-type'] === 'wx') {
handleProps(this) handleProps(this)
......
...@@ -6,11 +6,14 @@ import { ...@@ -6,11 +6,14 @@ import {
} from 'uni-shared' } from 'uni-shared'
import { import {
handleLink as handleBaseLink handleLink as handleBaseLink,
toSkip
} from '../../../mp-weixin/runtime/wrapper/util' } from '../../../mp-weixin/runtime/wrapper/util'
import deepEqual from './deep-equal' import deepEqual from './deep-equal'
export { markMPComponent } from '../../../mp-weixin/runtime/wrapper/util'
const customizeRE = /:/g const customizeRE = /:/g
const customize = cached((str) => { const customize = cached((str) => {
...@@ -145,9 +148,9 @@ export function handleRef (ref) { ...@@ -145,9 +148,9 @@ export function handleRef (ref) {
const refName = ref.props['data-ref'] const refName = ref.props['data-ref']
const refInForName = ref.props['data-ref-in-for'] const refInForName = ref.props['data-ref-in-for']
if (refName) { if (refName) {
this.$vm.$refs[refName] = ref.$vm || ref this.$vm.$refs[refName] = ref.$vm || toSkip(ref)
} else if (refInForName) { } else if (refInForName) {
(this.$vm.$refs[refInForName] || (this.$vm.$refs[refInForName] = [])).push(ref.$vm || ref) (this.$vm.$refs[refInForName] || (this.$vm.$refs[refInForName] = [])).push(ref.$vm || toSkip(ref))
} }
} }
......
import { import {
findVmByVueId, findVmByVueId,
initRefs as initRefsBase initRefs as initRefsBase,
toSkip
} from '../../../mp-weixin/runtime/wrapper/util' } from '../../../mp-weixin/runtime/wrapper/util'
export const mocks = ['__route__', '__webviewId__', '__nodeid__', '__nodeId__'] export const mocks = ['__route__', '__webviewId__', '__nodeid__', '__nodeId__']
...@@ -19,7 +20,7 @@ export function initRefs (vm) { ...@@ -19,7 +20,7 @@ export function initRefs (vm) {
mpInstance.selectAllComponents('.vue-ref', (components) => { mpInstance.selectAllComponents('.vue-ref', (components) => {
components.forEach(component => { components.forEach(component => {
const ref = component.dataset.ref const ref = component.dataset.ref
vm.$refs[ref] = component.$vm || component vm.$refs[ref] = component.$vm || toSkip(component)
}) })
}) })
mpInstance.selectAllComponents('.vue-ref-in-for', (forComponents) => { mpInstance.selectAllComponents('.vue-ref-in-for', (forComponents) => {
...@@ -28,7 +29,7 @@ export function initRefs (vm) { ...@@ -28,7 +29,7 @@ export function initRefs (vm) {
if (!vm.$refs[ref]) { if (!vm.$refs[ref]) {
vm.$refs[ref] = [] vm.$refs[ref] = []
} }
vm.$refs[ref].push(component.$vm || component) vm.$refs[ref].push(component.$vm || toSkip(component))
}) })
}) })
} }
......
...@@ -2,6 +2,7 @@ import { ...@@ -2,6 +2,7 @@ import {
cached, cached,
camelize camelize
} from 'uni-shared' } from 'uni-shared'
import { markMPComponent } from './wrapper/util'
const MPPage = Page const MPPage = Page
const MPComponent = Component const MPComponent = Component
...@@ -41,13 +42,10 @@ function initHook (name, options, isComponent) { ...@@ -41,13 +42,10 @@ function initHook (name, options, isComponent) {
isComponent && options.lifetimes && options.lifetimes[name] && (options = options.lifetimes) isComponent && options.lifetimes && options.lifetimes[name] && (options = options.lifetimes)
} }
const oldHook = options[name] const oldHook = options[name]
if (!oldHook) { options[name] = function (...args) {
options[name] = function () { markMPComponent(this)
initTriggerEvent(this) initTriggerEvent(this)
} if (oldHook) {
} else {
options[name] = function (...args) {
initTriggerEvent(this)
return oldHook.apply(this, args) return oldHook.apply(this, args)
} }
} }
......
import {
isObject
} from 'uni-shared'
export const mocks = ['__route__', '__wxExparserNodeId__', '__wxWebviewId__'] export const mocks = ['__route__', '__wxExparserNodeId__', '__wxWebviewId__']
export function findVmByVueId (vm, vuePid) { export function findVmByVueId (vm, vuePid) {
...@@ -35,7 +39,7 @@ function selectAllComponents (mpInstance, selector, $refs) { ...@@ -35,7 +39,7 @@ function selectAllComponents (mpInstance, selector, $refs) {
const components = mpInstance.selectAllComponents(selector) || [] const components = mpInstance.selectAllComponents(selector) || []
components.forEach(component => { components.forEach(component => {
const ref = component.dataset.ref const ref = component.dataset.ref
$refs[ref] = component.$vm || component $refs[ref] = component.$vm || toSkip(component)
if (__PLATFORM__ === 'mp-weixin') { if (__PLATFORM__ === 'mp-weixin') {
if (component.dataset.vueGeneric === 'scoped') { if (component.dataset.vueGeneric === 'scoped') {
component.selectAllComponents('.scoped-ref').forEach(scopedComponent => { component.selectAllComponents('.scoped-ref').forEach(scopedComponent => {
...@@ -78,7 +82,7 @@ export function initRefs (vm) { ...@@ -78,7 +82,7 @@ export function initRefs (vm) {
if (!$refs[ref]) { if (!$refs[ref]) {
$refs[ref] = [] $refs[ref] = []
} }
$refs[ref].push(component.$vm || component) $refs[ref].push(component.$vm || toSkip(component))
}) })
return syncRefs(refs, $refs) return syncRefs(refs, $refs)
} }
...@@ -102,4 +106,31 @@ export function handleLink (event) { ...@@ -102,4 +106,31 @@ export function handleLink (event) {
} }
vueOptions.parent = parentVm vueOptions.parent = parentVm
} }
export function markMPComponent (component) {
// 在 Vue 中标记为小程序组件
const IS_MP = '__v_isMPComponent'
Object.defineProperty(component, IS_MP, {
configurable: true,
enumerable: false,
value: true
})
return component
}
export 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
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册