Skip to content

Commit

Permalink
fix provide isn't reactive with a single array - Fix #5223
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingwl committed Mar 20, 2017
1 parent 84856a3 commit 3507eb8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/core/instance/inject.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* @flow */

import { hasSymbol } from 'core/util/env'
import { defineReactive } from '../observer/index'

export function initProvide (vm: Component) {
const provide = vm.$options.provide
Expand Down Expand Up @@ -29,7 +30,7 @@ export function initInjections (vm: Component) {
let source = vm
while (source) {
if (source._provided && provideKey in source._provided) {
vm[key] = source._provided[provideKey]
defineReactive(vm, key, source._provided[provideKey])
break
}
source = source.$parent
Expand Down
34 changes: 34 additions & 0 deletions test/unit/features/options/inject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,38 @@ describe('Options provide/inject', () => {
expect(vm.$el.textContent).toBe('123')
})
}

// Github issue #5223
it('should work with reactive array', done => {
const vm = new Vue({
template: `<div><child></child></div>`,
data () {
return {
foo: []
}
},
provide () {
return {
foo: this.foo
}
},
components: {
child: {
inject: ['foo'],
template: `<span>{{foo.length}}</span>`
}
}
}).$mount()

expect(vm.$el.innerHTML).toEqual(`<span>0</span>`)
vm.foo.push(vm.foo.length)
vm.$nextTick(() => {
expect(vm.$el.innerHTML).toEqual(`<span>1</span>`)
vm.foo.pop()
vm.$nextTick(() => {
expect(vm.$el.innerHTML).toEqual(`<span>0</span>`)
done()
})
})
})
})

0 comments on commit 3507eb8

Please sign in to comment.