From bd199675ff9264e44dde6601f10fcb95092b6756 Mon Sep 17 00:00:00 2001 From: tony Date: Wed, 2 Mar 2022 09:26:19 +0800 Subject: [PATCH 1/2] code style update --- packages/runtime-core/src/apiInject.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/runtime-core/src/apiInject.ts b/packages/runtime-core/src/apiInject.ts index c5c47876cb3..da1908c98e4 100644 --- a/packages/runtime-core/src/apiInject.ts +++ b/packages/runtime-core/src/apiInject.ts @@ -1,6 +1,5 @@ import { isFunction } from '@vue/shared' -import { currentInstance } from './component' -import { currentRenderingInstance } from './componentRenderContext' +import { currentInstance, getCurrentInstance } from './component' import { warn } from './warning' export interface InjectionKey extends Symbol {} @@ -45,14 +44,14 @@ export function inject( ) { // fallback to `currentRenderingInstance` so that this can be called in // a functional component - const instance = currentInstance || currentRenderingInstance + const instance = getCurrentInstance() if (instance) { // #2400 // to support `app.use` plugins, // fallback to appContext's `provides` if the instance is at root const provides = instance.parent == null - ? instance.vnode.appContext && instance.vnode.appContext.provides + ? instance.vnode.appContext?.provides : instance.parent.provides if (provides && (key as string | symbol) in provides) { From 70fd6c0b349e3f40827f336e9088c5b3bff160d8 Mon Sep 17 00:00:00 2001 From: tony Date: Thu, 3 Mar 2022 09:14:18 +0800 Subject: [PATCH 2/2] remove optional chain --- packages/runtime-core/src/apiInject.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/runtime-core/src/apiInject.ts b/packages/runtime-core/src/apiInject.ts index da1908c98e4..3cb92d1f2d9 100644 --- a/packages/runtime-core/src/apiInject.ts +++ b/packages/runtime-core/src/apiInject.ts @@ -51,7 +51,7 @@ export function inject( // fallback to appContext's `provides` if the instance is at root const provides = instance.parent == null - ? instance.vnode.appContext?.provides + ? instance.vnode.appContext && instance.vnode.appContext.provides : instance.parent.provides if (provides && (key as string | symbol) in provides) {