Skip to content

Commit 3bdcd6b

Browse files
authored
fix: access $store from nuxt2Context (#930)
1 parent 8876e60 commit 3bdcd6b

File tree

6 files changed

+38
-1
lines changed

6 files changed

+38
-1
lines changed

Diff for: packages/bridge/src/runtime/app.plugin.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ export default async (ctx, inject) => {
9999

100100
const proxiedApp = new Proxy(nuxtApp, {
101101
get (target, prop) {
102+
if (prop === '$store') {
103+
return target.nuxt2Context.store
104+
}
102105
if (prop[0] === '$') {
103106
return target.nuxt2Context[prop] || target.vue2App?.[prop]
104107
}

Diff for: playground/nuxt.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default defineNuxtConfig({
2929
})
3030
}
3131
],
32-
plugins: ['~/plugins/setup.js'],
32+
plugins: ['~/plugins/setup.js', '~/plugins/store.js'],
3333
nitro: {
3434
routeRules: {
3535
'/route-rules/spa': { ssr: false }

Diff for: playground/pages/store.vue

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script setup lang="ts">
2+
const { $store } = useNuxtApp()
3+
4+
const test = computed(() => $store.state.test)
5+
</script>
6+
7+
<template>
8+
<div>
9+
<p>state is: {{ test }}</p>
10+
</div>
11+
</template>

Diff for: playground/plugins/store.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default defineNuxtPlugin(() => {
2+
const { $store } = useNuxtApp()
3+
4+
$store.commit('setTest', '✅')
5+
})

Diff for: playground/store/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ export const actions = {
77
state.test = '✅'
88
}
99
}
10+
11+
export const mutations = {
12+
setTest (state, value) {
13+
state.test = value
14+
}
15+
}

Diff for: test/bridge.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ describe('legacy capi', () => {
121121
expect(html).toMatch(/([\s\S]*){11}/)
122122
await expectNoClientErrors('/legacy-capi')
123123
})
124+
125+
it('should be changed store.state', async () => {
126+
const html = await $fetch('/legacy-capi')
127+
expect(html).toContain('<tr><td><b>useStore</b></td><td> ✅</td></tr>')
128+
})
124129
})
125130

126131
describe('errors', () => {
@@ -191,6 +196,13 @@ describe('middleware', () => {
191196
})
192197
})
193198

199+
describe('store', () => {
200+
it('should be able to access $store in plugins', async () => {
201+
const html = await $fetch('/store')
202+
expect(html).toContain('state is: ✅')
203+
})
204+
})
205+
194206
describe('nitro plugins', () => {
195207
it('should prepend a node to the rendered template', async () => {
196208
const html = await $fetch('/nitro/template-plugin')

0 commit comments

Comments
 (0)