diff --git a/packages/pinia/src/devtools/plugin.ts b/packages/pinia/src/devtools/plugin.ts index e122a04d1b..2c9fdd5c70 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 +}