Skip to content

add provide/inject on functional context #5204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 5, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions 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 { extend } from 'shared/util'

export function initProvide (vm: Component) {
const provide = vm.$options.provide
Expand All @@ -12,11 +13,16 @@ export function initProvide (vm: Component) {
}

export function initInjections (vm: Component) {
const inject: any = vm.$options.inject
const result = resolveInject(vm.$options.inject, vm)
extend(vm, result)
}

export function resolveInject (inject: any, vm: Component): ?Object {
if (inject) {
// inject is :any because flow is not smart enough to figure out cached
// isArray here
const isArray = Array.isArray(inject)
const result = Object.create(null)
const keys = isArray
? inject
: hasSymbol
Expand All @@ -29,11 +35,12 @@ export function initInjections (vm: Component) {
let source = vm
while (source) {
if (source._provided && provideKey in source._provided) {
vm[key] = source._provided[provideKey]
result[key] = source._provided[provideKey]
break
}
source = source.$parent
}
}
return result
}
}
3 changes: 3 additions & 0 deletions src/core/vdom/create-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import VNode from './vnode'
import { createElement } from './create-element'
import { resolveConstructorOptions } from '../instance/init'
import { resolveSlots } from '../instance/render-helpers/resolve-slots'
import { resolveInject } from '../instance/inject'

import {
warn,
Expand Down Expand Up @@ -183,11 +184,13 @@ function createFunctionalComponent (
// gets a unique context - this is necessary for correct named slot check
const _context = Object.create(context)
const h = (a, b, c, d) => createElement(_context, a, b, c, d, true)
const injections = resolveInject(Ctor.options.inject, _context)
const vnode = Ctor.options.render.call(null, h, {
props,
data,
parent: context,
children,
injections,
slots: () => resolveSlots(children, context)
})
if (vnode instanceof VNode) {
Expand Down
23 changes: 23 additions & 0 deletions test/unit/features/options/inject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,29 @@ describe('Options provide/inject', () => {
expect(child.baz).toBe(3)
})

// Github issue #5194
it('should work with functional', () => {
new Vue({
template: `<child/>`,
provide: {
foo: 1,
bar: false
},
components: {
child: {
functional: true,
inject: ['foo', 'bar'],
render (h, context) {
const { injections } = context
injected = [injections.foo, injections.bar]
}
}
}
}).$mount()

expect(injected).toEqual([1, false])
})

if (typeof Reflect !== 'undefined' && isNative(Reflect.ownKeys)) {
it('with Symbol keys', () => {
const s = Symbol()
Expand Down
1 change: 1 addition & 0 deletions types/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface RenderContext {
slots(): any;
data: VNodeData;
parent: Vue;
injections: any
}

export interface PropOptions {
Expand Down