From: Fan Pei Date: Wed, 26 Jun 2024 09:27:15 +0000 (+0900) Subject: feat(devtools): expose selected store as global variable (#2692) X-Git-Tag: @pinia/nuxt@0.5.2~25 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=e0a73512af38050111ecbcf881908541aff9414d;p=thirdparty%2Fvuejs%2Fpinia.git feat(devtools): expose selected store as global variable (#2692) * feat: expose selected store as * fix: Expose and to window * refactor: add to globalThis --------- Co-authored-by: Eduardo San Martin Morote --- diff --git a/packages/pinia/src/devtools/plugin.ts b/packages/pinia/src/devtools/plugin.ts index e122a04d..2c9fdd5c 100644 --- a/packages/pinia/src/devtools/plugin.ts +++ b/packages/pinia/src/devtools/plugin.ts @@ -216,6 +216,9 @@ export function registerPiniaDevtools(app: DevtoolsApp, pinia: Pinia) { } }) + // Expose pinia instance as $pinia to window + globalThis.$pinia = pinia + api.on.getInspectorState((payload) => { if (payload.app === app && payload.inspectorId === INSPECTOR_ID) { const inspectedStore = @@ -230,6 +233,9 @@ export function registerPiniaDevtools(app: DevtoolsApp, pinia: Pinia) { } if (inspectedStore) { + // Expose selected store as $store to window + if (payload.nodeId !== PINIA_ROOT_ID) + globalThis.$store = toRaw(inspectedStore as StoreGeneric) payload.state = formatStoreForInspectorState(inspectedStore) } } @@ -592,3 +598,14 @@ export function devtoolsPlugin< store as StoreGeneric ) } + +declare global { + /** + * Exposes the `pinia` instance when Devtools are opened. + */ + var $pinia: Pinia | undefined + /** + * Exposes the current store when Devtools are opened. + */ + var $store: StoreGeneric | undefined +}