-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export function initInjections (vm) { | ||
const { provide, inject } = vm.$options | ||
if (provide) { | ||
vm._provided = typeof provide === 'function' | ||
? provide.call(vm) | ||
: provide | ||
} | ||
if (inject) { | ||
const isArray = Array.isArray(inject) | ||
const keys = isArray ? inject : Object.keys(inject) | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i] | ||
const provideKey = isArray ? key : inject[key] | ||
let source = vm | ||
while (source) { | ||
if (source._provided && source._provided[provideKey]) { | ||
vm[key] = source._provided[provideKey] | ||
break | ||
} | ||
source = source.$parent | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import Vue from 'vue' | ||
|
||
describe('Options provide/inject', () => { | ||
let injected | ||
const injectedComp = { | ||
inject: ['foo', 'bar'], | ||
render () {}, | ||
created () { | ||
injected = [this.foo, this.bar] | ||
} | ||
} | ||
|
||
beforeEach(() => { | ||
injected = null | ||
}) | ||
|
||
it('should work', () => { | ||
new Vue({ | ||
template: `<child/>`, | ||
provide: { | ||
foo: 1, | ||
bar: 2 | ||
}, | ||
components: { | ||
child: { | ||
template: `<injected-comp/>`, | ||
components: { | ||
injectedComp | ||
} | ||
} | ||
} | ||
}).$mount() | ||
|
||
expect(injected).toEqual([1, 2]) | ||
}) | ||
|
||
it('should use closest parent', () => { | ||
new Vue({ | ||
template: `<child/>`, | ||
provide: { | ||
foo: 1, | ||
bar: 2 | ||
}, | ||
components: { | ||
child: { | ||
provide: { | ||
foo: 3 | ||
}, | ||
template: `<injected-comp/>`, | ||
components: { | ||
injectedComp | ||
} | ||
} | ||
} | ||
}).$mount() | ||
|
||
expect(injected).toEqual([3, 2]) | ||
}) | ||
|
||
it('provide function', () => { | ||
new Vue({ | ||
template: `<child/>`, | ||
data: { | ||
a: 1, | ||
b: 2 | ||
}, | ||
provide () { | ||
return { | ||
foo: this.a, | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
yyx990803
Author
Member
|
||
bar: this.b | ||
} | ||
}, | ||
components: { | ||
child: { | ||
template: `<injected-comp/>`, | ||
components: { | ||
injectedComp | ||
} | ||
} | ||
} | ||
}).$mount() | ||
|
||
expect(injected).toEqual([1, 2]) | ||
}) | ||
|
||
it('inject with alias', () => { | ||
const injectAlias = { | ||
inject: { | ||
baz: 'foo', | ||
qux: 'bar' | ||
}, | ||
render () {}, | ||
created () { | ||
injected = [this.baz, this.qux] | ||
} | ||
} | ||
|
||
new Vue({ | ||
template: `<child/>`, | ||
provide: { | ||
foo: 1, | ||
bar: 2 | ||
}, | ||
components: { | ||
child: { | ||
template: `<inject-alias/>`, | ||
components: { | ||
injectAlias | ||
} | ||
} | ||
} | ||
}).$mount() | ||
|
||
expect(injected).toEqual([1, 2]) | ||
}) | ||
|
||
it('self-inject', () => { | ||
const vm = new Vue({ | ||
provide: { | ||
foo: 1 | ||
}, | ||
inject: ['foo'] | ||
}) | ||
|
||
expect(vm.foo).toBe(1) | ||
}) | ||
}) |
3 comments
on commit f916bcf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking forward to this. Been missing this feature as i come from React. When using React i used a plugin called redux-form which depends on context. I want to try to make something similar to Vue once this is ready! Great work!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already in the latest 2.2 release ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes, i see! Came here via some googling on my phone so i didn't realise. Thanks. Will upgrade to 2.2! 👍
If
a
is an object, will it be reactive in the component that gets injection?