Skip to content

Commit

Permalink
fix(core): should preserve reactivity-ness of injected objects
Browse files Browse the repository at this point in the history
fix #5913
  • Loading branch information
yyx990803 committed Jul 10, 2017
1 parent 5dbca4e commit 8d66691
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/core/instance/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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') {
Expand All @@ -32,6 +33,7 @@ export function initInjections (vm: Component) {
defineReactive(vm, key, result[key])
}
})
observerState.shouldConvert = true
}
}

Expand Down
45 changes: 44 additions & 1 deletion test/unit/features/options/inject.spec.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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: `<div><child ref='child'></child></div>`,
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: `<span/>`
}
}
}).$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)
})
})

0 comments on commit 8d66691

Please sign in to comment.