mirror of https://github.com/vuejs/core.git
feat: hasInjectionContext() for libraries (#8111)
This commit is contained in:
parent
2f9f6eceb9
commit
5510ce385a
|
@ -8,9 +8,10 @@ import {
|
||||||
Ref,
|
Ref,
|
||||||
readonly,
|
readonly,
|
||||||
reactive,
|
reactive,
|
||||||
defineComponent
|
defineComponent,
|
||||||
|
hasInjectionContext
|
||||||
} from '../src/index'
|
} 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
|
// reference: https://vue-composition-api-rfc.netlify.com/api.html#provide-inject
|
||||||
describe('api: provide/inject', () => {
|
describe('api: provide/inject', () => {
|
||||||
|
@ -347,4 +348,30 @@ describe('api: provide/inject', () => {
|
||||||
render(h(Comp), root)
|
render(h(Comp), root)
|
||||||
expect(serialize(root)).toBe(`<div><!----></div>`)
|
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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -73,3 +73,12 @@ export function inject(
|
||||||
warn(`inject() can only be used inside setup() or functional components.`)
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -56,7 +56,7 @@ export {
|
||||||
onErrorCaptured,
|
onErrorCaptured,
|
||||||
onServerPrefetch
|
onServerPrefetch
|
||||||
} from './apiLifecycle'
|
} from './apiLifecycle'
|
||||||
export { provide, inject } from './apiInject'
|
export { provide, inject, hasInjectionContext } from './apiInject'
|
||||||
export { nextTick } from './scheduler'
|
export { nextTick } from './scheduler'
|
||||||
export { defineComponent } from './apiDefineComponent'
|
export { defineComponent } from './apiDefineComponent'
|
||||||
export { defineAsyncComponent } from './apiAsyncComponent'
|
export { defineAsyncComponent } from './apiAsyncComponent'
|
||||||
|
|
Loading…
Reference in New Issue