diff --git a/src/core/instance/inject.js b/src/core/instance/inject.js index 239edf8d3a..4ee7631990 100644 --- a/src/core/instance/inject.js +++ b/src/core/instance/inject.js @@ -3,7 +3,7 @@ import { warn } from '../util/index' import { hasOwn } from 'shared/util' import { hasSymbol } from 'core/util/env' -import { defineReactive } from '../observer/index' +import { defineReactive, observerState } from '../observer/index' export function initProvide (vm: Component) { const provide = vm.$options.provide @@ -17,6 +17,7 @@ export function initProvide (vm: Component) { export function initInjections (vm: Component) { const result = resolveInject(vm.$options.inject, vm) if (result) { + observerState.shouldConvert = false Object.keys(result).forEach(key => { /* istanbul ignore else */ if (process.env.NODE_ENV !== 'production') { @@ -32,6 +33,7 @@ export function initInjections (vm: Component) { defineReactive(vm, key, result[key]) } }) + observerState.shouldConvert = true } } diff --git a/test/unit/features/options/inject.spec.js b/test/unit/features/options/inject.spec.js index e3ba23d488..ba95d949a0 100644 --- a/test/unit/features/options/inject.spec.js +++ b/test/unit/features/options/inject.spec.js @@ -1,5 +1,6 @@ import Vue from 'vue' -import { isNative } from 'core/util/env' +import { Observer } from 'core/observer/index' +import { isNative, isObject, hasOwn } from 'core/util/index' describe('Options provide/inject', () => { let injected @@ -399,4 +400,46 @@ describe('Options provide/inject', () => { expect(injected).toEqual(['foo', 'bar']) }) + + // #5913 + it('should keep the reactive with provide', () => { + function isObserver (obj) { + if (isObject(obj)) { + return hasOwn(obj, '__ob__') && obj.__ob__ instanceof Observer + } + return false + } + + const vm = new Vue({ + template: `
`, + data () { + return { + foo: {}, + $foo: {}, + foo1: [] + } + }, + provide () { + return { + foo: this.foo, + $foo: this.$foo, + foo1: this.foo1, + bar: {}, + baz: [] + } + }, + components: { + child: { + inject: ['foo', '$foo', 'foo1', 'bar', 'baz'], + template: `` + } + } + }).$mount() + const child = vm.$refs.child + expect(isObserver(child.foo)).toBe(true) + expect(isObserver(child.$foo)).toBe(false) + expect(isObserver(child.foo1)).toBe(true) + expect(isObserver(child.bar)).toBe(false) + expect(isObserver(child.baz)).toBe(false) + }) })