| 45 | treatDefaultAsFactory: true, |
| 46 | ): T |
| 47 | export function inject( |
| 48 | key: InjectionKey<any> | string, |
| 49 | defaultValue?: unknown, |
| 50 | treatDefaultAsFactory = false, |
| 51 | ) { |
| 52 | // fallback to `currentRenderingInstance` so that this can be called in |
| 53 | // a functional component |
| 54 | const instance = getCurrentInstance() |
| 55 | |
| 56 | // also support looking up from app-level provides w/ `app.runWithContext()` |
| 57 | if (instance || currentApp) { |
| 58 | // #2400 |
| 59 | // to support `app.use` plugins, |
| 60 | // fallback to appContext's `provides` if the instance is at root |
| 61 | // #11488, in a nested createApp, prioritize using the provides from currentApp |
| 62 | // #13212, for custom elements we must get injected values from its appContext |
| 63 | // as it already inherits the provides object from the parent element |
| 64 | let provides = currentApp |
| 65 | ? currentApp._context.provides |
| 66 | : instance |
| 67 | ? instance.parent == null || instance.ce |
| 68 | ? instance.vnode.appContext && instance.vnode.appContext.provides |
| 69 | : instance.parent.provides |
| 70 | : undefined |
| 71 | |
| 72 | if (provides && (key as string | symbol) in provides) { |
| 73 | // TS doesn't allow symbol as index type |
| 74 | return provides[key as string] |
| 75 | } else if (arguments.length > 1) { |
| 76 | return treatDefaultAsFactory && isFunction(defaultValue) |
| 77 | ? defaultValue.call(instance && instance.proxy) |
| 78 | : defaultValue |
| 79 | } else if (__DEV__) { |
| 80 | warn(`injection "${String(key)}" not found.`) |
| 81 | } |
| 82 | } else if (__DEV__) { |
| 83 | warn(`inject() can only be used inside setup() or functional components.`) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns true if `inject()` can be used without warning about being called in the wrong place (e.g. outside of |