fix(vue-app): use getter to provide this.$nuxt (#8170)

This commit is contained in:
Daniel Roe 2020-10-13 11:19:08 +01:00 committed by GitHub
parent a52b5af694
commit 9c4dac3c61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 1 deletions

View File

@ -98,7 +98,8 @@ export default {
},
created () {
// Add this.$nuxt in child instances
Vue.prototype.<%= globals.nuxt %> = this
this.$root.$options.<%= globals.nuxt %> = this
if (process.client) {
// add to window so we can listen when ready
window.<%= globals.nuxt %> = <%= (globals.nuxt !== '$nuxt' ? 'window.$nuxt = ' : '') %>this

View File

@ -44,6 +44,13 @@ Vue.component(NuxtChild.name, NuxtChild)
// Component: <Nuxt>
Vue.component(Nuxt.name, Nuxt)
Object.defineProperty(Vue.prototype, '<%= globals.nuxt %>', {
get() {
return this.$root.$options.<%= globals.nuxt %>
},
configurable: true
})
<% if (features.meta) {
// vue-meta configuration
const vueMetaOptions = {

View File

@ -110,6 +110,19 @@ describe('ssr', () => {
await stressTest('/asyncData')
})
test('does not share state', async () => {
const [page1, page2] = await Promise.all([
nuxt.server.renderRoute('/context'),
nuxt.server.renderRoute('/context/child')
])
expect(page1.html).toContain('vm: /context')
expect(page1.html).toContain('context: /context')
expect(page2.html).toContain('vm: /context/child')
expect(page2.html).toContain('context: /context/child')
})
// Close server and ask nuxt to stop listening to file changes
afterAll(async () => {
await nuxt.close()

10
test/fixtures/ssr/pages/context.vue vendored Normal file
View File

@ -0,0 +1,10 @@
<template>
<div>
<section data-test="parent">
<pre>vm: {{ $route.fullPath }}</pre>
<pre>context: {{ $nuxt.context.route.fullPath }}</pre>
</section>
<NuxtChild />
</div>
</template>

View File

@ -0,0 +1,6 @@
<template>
<section data-test="child">
<pre>vm: {{ $route.fullPath }}</pre>
<pre>context: {{ $nuxt.context.route.fullPath }}</pre>
</section>
</template>