diff --git a/packages/bridge/src/runtime/app.plugin.mjs b/packages/bridge/src/runtime/app.plugin.mjs index 43bfe915..292e1dcb 100644 --- a/packages/bridge/src/runtime/app.plugin.mjs +++ b/packages/bridge/src/runtime/app.plugin.mjs @@ -99,6 +99,9 @@ export default async (ctx, inject) => { const proxiedApp = new Proxy(nuxtApp, { get (target, prop) { + if (prop === '$store') { + return target.nuxt2Context.store + } if (prop[0] === '$') { return target.nuxt2Context[prop] || target.vue2App?.[prop] } diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index 26555525..99da054d 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -29,7 +29,7 @@ export default defineNuxtConfig({ }) } ], - plugins: ['~/plugins/setup.js'], + plugins: ['~/plugins/setup.js', '~/plugins/store.js'], nitro: { routeRules: { '/route-rules/spa': { ssr: false } diff --git a/playground/pages/store.vue b/playground/pages/store.vue new file mode 100644 index 00000000..61adb157 --- /dev/null +++ b/playground/pages/store.vue @@ -0,0 +1,11 @@ + + + diff --git a/playground/plugins/store.js b/playground/plugins/store.js new file mode 100644 index 00000000..1b387345 --- /dev/null +++ b/playground/plugins/store.js @@ -0,0 +1,5 @@ +export default defineNuxtPlugin(() => { + const { $store } = useNuxtApp() + + $store.commit('setTest', '✅') +}) diff --git a/playground/store/index.js b/playground/store/index.js index 96412b0d..df7cb02c 100644 --- a/playground/store/index.js +++ b/playground/store/index.js @@ -7,3 +7,9 @@ export const actions = { state.test = '✅' } } + +export const mutations = { + setTest (state, value) { + state.test = value + } +} diff --git a/test/bridge.test.ts b/test/bridge.test.ts index 225eea98..96488588 100644 --- a/test/bridge.test.ts +++ b/test/bridge.test.ts @@ -121,6 +121,11 @@ describe('legacy capi', () => { expect(html).toMatch(/([\s\S]*✅){11}/) await expectNoClientErrors('/legacy-capi') }) + + it('should be changed store.state', async () => { + const html = await $fetch('/legacy-capi') + expect(html).toContain('useStore ✅') + }) }) describe('errors', () => { @@ -191,6 +196,13 @@ describe('middleware', () => { }) }) +describe('store', () => { + it('should be able to access $store in plugins', async () => { + const html = await $fetch('/store') + expect(html).toContain('state is: ✅') + }) +}) + describe('nitro plugins', () => { it('should prepend a node to the rendered template', async () => { const html = await $fetch('/nitro/template-plugin')