Skip to content

Commit

Permalink
fix: fix a bug of Options API jsonld
Browse files Browse the repository at this point in the history
Fix a bug which Options API jsonld method can not access `this` context
  • Loading branch information
ymmooot committed Apr 18, 2022
1 parent 00ff0f0 commit 48e5ef1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
3 changes: 2 additions & 1 deletion example/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
<h1>Product List</h1>
<code v-html="jsonld"></code>

<nuxt-link :to="{ name: 'static' }">static</nuxt-link>
<ul>
<li v-for="p in products" :key="p.id">
name:
<nuxt-link :to="{ name: 'products-id', params: { id: p.name.toLowerCase() } }">
{{ p.name }}
</nuxt-link>
</li>
<li><nuxt-link :to="{ name: 'static' }">Static JSON</nuxt-link></li>
<li><nuxt-link :to="{ name: 'option' }">Options API</nuxt-link></li>
</ul>
</div>
</template>
Expand Down
31 changes: 31 additions & 0 deletions example/pages/option.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<template>
<div>
<h1>Options API</h1>
<code v-html="jsonld"></code>
<div>
<p>{{ count }}</p>
<button @click="count++">+</button>
</div>
<nuxt-link to="/"> Back to list </nuxt-link>
</div>
</template>

<script>
import { getJsonldForDemo } from '@/mixins';
export default {
mixins: [getJsonldForDemo],
data() {
return {
count: 0,
};
},
jsonld() {
return {
'@context': 'https://schema.org',
'@type': 'Thing',
name: this.count,
};
},
};
</script>
2 changes: 1 addition & 1 deletion src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useHead } from '#head';

export default defineNuxtPlugin((nuxtApp) => {
const mixin = {
beforeCreate(this) {
created() {
if (typeof this.$options?.jsonld !== 'function') {
return;
}
Expand Down
6 changes: 3 additions & 3 deletions test/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ describe('plugin', () => {
};
plugin(nuxtApp);
expect(nuxtApp.vueApp.use).toBeCalledTimes(1);
let beforeCreate;
let created;
const vueMock = {
mixin: jest.fn().mockImplementation((arg) => {
beforeCreate = arg.beforeCreate;
created = arg.created;
}),
};
installFunction(vueMock);

// mixin is set
expect(vueMock.mixin).toBeCalled();

beforeCreate.call({
created.call({
$options: {
jsonld,
},
Expand Down

0 comments on commit 48e5ef1

Please sign in to comment.