Skip to content

Commit

Permalink
feat: rename inject alias from "name" to "from"
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Oct 6, 2017
1 parent 6dac3db commit 6893499
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/core/instance/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function resolveInject (inject: any, vm: Component): ?Object {

for (let i = 0; i < keys.length; i++) {
const key = keys[i]
const provideKey = inject[key].name
const provideKey = inject[key].from
let source = vm
while (source) {
if (source._provided && provideKey in source._provided) {
Expand Down
6 changes: 3 additions & 3 deletions src/core/util/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,14 @@ function normalizeInject (options: Object) {
const normalized = options.inject = {}
if (Array.isArray(inject)) {
for (let i = 0; i < inject.length; i++) {
normalized[inject[i]] = { name: inject[i] }
normalized[inject[i]] = { from: inject[i] }
}
} else if (isPlainObject(inject)) {
for (const key in inject) {
const val = inject[key]
normalized[key] = isPlainObject(val)
? extend({ name: key }, val)
: { name: val }
? extend({ from: key }, val)
: { from: val }
}
}
}
Expand Down
23 changes: 20 additions & 3 deletions test/unit/features/options/inject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,29 @@ describe('Options provide/inject', () => {
injected = [this.foo, this.bar, this.baz]
}
})
expect(`Injection "foo" not found`).not.toHaveBeenWarned()
expect(`Injection "bar" not found`).not.toHaveBeenWarned()
expect(`Injection "baz" not found`).not.toHaveBeenWarned()
expect(injected).toEqual([1, false, undefined])
})

it('should support name alias and default together', () => {
const vm = new Vue({
provide: {
FOO: 2
}
})
new Vue({
parent: vm,
inject: {
foo: { from: 'FOO', default: 1 },
bar: { default: false },
baz: { default: undefined }
},
created () {
injected = [this.foo, this.bar, this.baz]
}
})
expect(injected).toEqual([2, false, undefined])
})

it('should use provided value even if inject has default', () => {
const vm = new Vue({
provide: {
Expand Down

0 comments on commit 6893499

Please sign in to comment.