| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,17 +48,18 @@ export function inject( | |||
| 48 | 48 | defaultValue?: unknown, | |
| 49 | 49 | treatDefaultAsFactory = false | |
| 50 | 50 | ) { | |
| 51 | - if (!key) { | ||
| 52 | - return defaultValue | ||
| 53 | - } | ||
| 54 | - | ||
| 55 | 51 | const vm = getCurrentInstance()?.proxy | |
| 56 | 52 | if (!vm) { | |
| 57 | 53 | __DEV__ && | |
| 58 | 54 | warn(`inject() can only be used inside setup() or functional components.`) | |
| 59 | 55 | return | |
| 60 | 56 | } | |
| 61 | 57 | ||
| 58 | + if (!key) { | ||
| 59 | + __DEV__ && warn(`injection "${String(key)}" not found.`, vm) | ||
| 60 | + return defaultValue | ||
| 61 | + } | ||
| 62 | + | ||
| 62 | 63 | const val = resolveInject(key, vm) | |
| 63 | 64 | if (val !== NOT_FOUND) { | |
| 64 | 65 | return val | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -242,6 +242,28 @@ describe('api: provide/inject', () => { | |||
| 242 | 242 | expect(`[Vue warn]: Injection "foo" not found`).toHaveBeenWarned() | |
| 243 | 243 | }) | |
| 244 | 244 | ||
| 245 | + it('should warn unfound w/ injectionKey is undefined', () => { | ||
| 246 | + const Provider = { | ||
| 247 | + setup() { | ||
| 248 | + return () => h(Consumer) | ||
| 249 | + }, | ||
| 250 | + } | ||
| 251 | + | ||
| 252 | + const Consumer = { | ||
| 253 | + setup() { | ||
| 254 | + // The emulation does not use TypeScript | ||
| 255 | + const foo = inject(undefined as unknown as string) | ||
| 256 | + expect(foo).toBeUndefined() | ||
| 257 | + return () => h('div', foo as unknown as string) | ||
| 258 | + }, | ||
| 259 | + } | ||
| 260 | + | ||
| 261 | + const root = document.createElement('div') | ||
| 262 | + const vm = createApp(Provider).mount(root) | ||
| 263 | + expect(vm.$el.outerHTML).toBe(`<div></div>`) | ||
| 264 | + expect(`[Vue warn]: injection "undefined" not found.`).toHaveBeenWarned() | ||
| 265 | + }) | ||
| 266 | + | ||
| 245 | 267 | it('should not self-inject', () => { | |
| 246 | 268 | const Comp = { | |
| 247 | 269 | setup() { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments