mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +00:00
feat(bridge): auto-inject based on provide
key from plugins (#3536)
This commit is contained in:
parent
f1d2eb1335
commit
856c01ab42
@ -74,8 +74,20 @@ export function callWithNuxt<T extends (...args: any[]) => any> (nuxt: NuxtAppCo
|
||||
return p
|
||||
}
|
||||
|
||||
export function defineNuxtPlugin (plugin: (nuxtApp: NuxtAppCompat) => void): (ctx: Context) => void {
|
||||
return ctx => callWithNuxt(ctx.$_nuxtApp, plugin, [ctx.$_nuxtApp])
|
||||
interface Plugin {
|
||||
(nuxt: NuxtAppCompat): Promise<void> | Promise<{ provide?: Record<string, any> }> | void | { provide?: Record<string, any> }
|
||||
}
|
||||
|
||||
export function defineNuxtPlugin (plugin: Plugin): (ctx: Context, inject: (id: string, value: any) => void) => void {
|
||||
return async (ctx, inject) => {
|
||||
const result = await callWithNuxt(ctx.$_nuxtApp, plugin, [ctx.$_nuxtApp])
|
||||
if (result && result.provide) {
|
||||
for (const key in result.provide) {
|
||||
inject(key, result.provide[key])
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
export const useNuxtApp = (): NuxtAppCompat => {
|
||||
|
8
test/fixtures/bridge/plugins/setup.js
vendored
8
test/fixtures/bridge/plugins/setup.js
vendored
@ -2,10 +2,14 @@ import { onGlobalSetup, ref } from '@nuxtjs/composition-api'
|
||||
|
||||
import { defineNuxtPlugin } from '#app'
|
||||
|
||||
export default defineNuxtPlugin((nuxtApp) => {
|
||||
export default defineNuxtPlugin(() => {
|
||||
const globalsetup = ref('🚧')
|
||||
onGlobalSetup(() => {
|
||||
globalsetup.value = '✅'
|
||||
})
|
||||
nuxtApp.provide('globalsetup', globalsetup)
|
||||
return {
|
||||
provide: {
|
||||
globalsetup
|
||||
}
|
||||
}
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user