]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat: hasInjectionContext() for libraries (#8111)
authorEduardo San Martin Morote <posva@users.noreply.github.com>
Thu, 20 Apr 2023 02:12:18 +0000 (04:12 +0200)
committerGitHub <noreply@github.com>
Thu, 20 Apr 2023 02:12:18 +0000 (10:12 +0800)
packages/runtime-core/__tests__/apiInject.spec.ts
packages/runtime-core/src/apiInject.ts
packages/runtime-core/src/index.ts

index 87a415aa972b42d38058fba444ad3539ea34e80e..a7aae7ebfa9853e4ab767788a509369e048bbb96 100644 (file)
@@ -8,9 +8,10 @@ import {
   Ref,
   readonly,
   reactive,
-  defineComponent
+  defineComponent,
+  hasInjectionContext
 } from '../src/index'
-import { render, nodeOps, serialize } from '@vue/runtime-test'
+import { render, nodeOps, serialize, createApp } from '@vue/runtime-test'
 
 // reference: https://vue-composition-api-rfc.netlify.com/api.html#provide-inject
 describe('api: provide/inject', () => {
@@ -347,4 +348,30 @@ describe('api: provide/inject', () => {
     render(h(Comp), root)
     expect(serialize(root)).toBe(`<div><!----></div>`)
   })
+
+  describe('hasInjectionContext', () => {
+    it('should be false outside of setup', () => {
+      expect(hasInjectionContext()).toBe(false)
+    })
+
+    it('should be true within setup', () => {
+      expect.assertions(1)
+      const Comp = {
+        setup() {
+          expect(hasInjectionContext()).toBe(true)
+          return () => null
+        }
+      }
+
+      const root = nodeOps.createElement('div')
+      render(h(Comp), root)
+    })
+
+    it('should be true within app.runWithContext()', () => {
+      expect.assertions(1)
+      createApp({}).runWithContext(() => {
+        expect(hasInjectionContext()).toBe(true)
+      })
+    })
+  })
 })
index 6eedee88c0956b9abd5bcb419253efdef7b59b31..4559c1b702f2e3492d39493c1a4a7f1844d9310a 100644 (file)
@@ -73,3 +73,12 @@ export function inject(
     warn(`inject() can only be used inside setup() or functional components.`)
   }
 }
+
+/**
+ * Returns true if `inject()` can be used without warning about being called in the wrong place (e.g. outside of
+ * setup()). This is used by libraries that want to use `inject()` internally without triggering a warning to the end
+ * user. One example is `useRoute()` in `vue-router`.
+ */
+export function hasInjectionContext(): boolean {
+  return !!(currentInstance || currentRenderingInstance || currentApp)
+}
index e427773a70e84f6f9c9df5a439753fb1b27617f4..a115b0179c11be951337666c7a502967987e9468 100644 (file)
@@ -56,7 +56,7 @@ export {
   onErrorCaptured,
   onServerPrefetch
 } from './apiLifecycle'
-export { provide, inject } from './apiInject'
+export { provide, inject, hasInjectionContext } from './apiInject'
 export { nextTick } from './scheduler'
 export { defineComponent } from './apiDefineComponent'
 export { defineAsyncComponent } from './apiAsyncComponent'