Skip to content
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

Merge inject when extending a component #5827

Merged
merged 9 commits into from
Jun 15, 2017
26 changes: 26 additions & 0 deletions src/core/util/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ strats.watch = function (parentVal: ?Object, childVal: ?Object): ?Object {
*/
strats.props =
strats.methods =
strats.inject =
strats.computed = function (parentVal: ?Object, childVal: ?Object): ?Object {
if (!childVal) return Object.create(parentVal || null)
if (!parentVal) return childVal
Expand Down Expand Up @@ -247,6 +248,30 @@ function normalizeProps (options: Object) {
options.props = res
}

/**
* Normalize all injections into Object-based format
*/
function normalizeInject (options: Object) {
const inject = options.inject
if (!inject) return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The normalization is only needed if inject is an Array, so we can just return if it is not an Array.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition, with normalization here, we should be able to simplify the resolveInject function here since the normalized value will always be objects: https://github.com/vuejs/vue/blob/dev/src/core/instance/inject.js#L38

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, will look into it later.

let res = {}
let i, val
if (Array.isArray(inject)) {
i = inject.length
while (i--) {
val = inject[i]
if (typeof val === 'string') {
res[val] = val
} else if (process.env.NODE_ENV !== 'production') {
warn('inject must be strings when using array syntax.')
}
}
} else if (isPlainObject(inject)) {
res = options.inject
}
options.inject = res
}

/**
* Normalize raw function directives into object format.
*/
Expand Down Expand Up @@ -280,6 +305,7 @@ export function mergeOptions (
}

normalizeProps(child)
normalizeInject(child)
normalizeDirectives(child)
const extendsFrom = child.extends
if (extendsFrom) {
Expand Down