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
Changes from all commits
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
23 changes: 22 additions & 1 deletion src/core/instance/inject.js
Original file line number Diff line number Diff line change
@@ -14,11 +14,31 @@ export function initProvide (vm: Component) {
}

export function initInjections (vm: Component) {
const inject: any = vm.$options.inject
const result = resolveInject(vm.$options.inject, vm)
if (result) {
Object.keys(result).forEach(key => {
if (process.env.NODE_ENV !== 'production') {
defineReactive(vm, key, result[key], () => {
warn(
`Avoid mutating an injected value directly since the changes will be ` +
`overwritten whenever the provided component re-renders. ` +
`injection being mutated: "${key}"`,
vm
)
})
} else {
defineReactive(vm, key, result[key])
}
})
}
}

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
@@ -49,5 +69,6 @@ export function initInjections (vm: Component) {
source = source.$parent
}
}
return result
}
}
5 changes: 5 additions & 0 deletions src/core/vdom/create-component.js
Original file line number Diff line number Diff line change
@@ -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,
@@ -184,11 +185,15 @@ 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)

// functional injections should not reactive
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) {
23 changes: 23 additions & 0 deletions test/unit/features/options/inject.spec.js
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions types/options.d.ts
Original file line number Diff line number Diff line change
@@ -69,6 +69,7 @@ export interface RenderContext {
slots(): any;
data: VNodeData;
parent: Vue;
injections: any
}

export interface PropOptions {