File tree Expand file tree Collapse file tree 7 files changed +70
-121
lines changed Expand file tree Collapse file tree 7 files changed +70
-121
lines changed Original file line number Diff line number Diff line change 1- import type { NuxtApp } from '#app' ;
2-
31type Dog = {
42 breed : string ;
53 name : string ;
@@ -17,18 +15,18 @@ export const useDog = async () => {
1715 age : 5 ,
1816 } ;
1917
18+ // Note: This jsonld will not disappear even after page transition.
19+ // If you want to link it to the page, use useJsonld in the component side.
2020 app . runWithContext ( ( ) => {
2121 useJsonld ( ( ) => ( {
2222 '@context' : 'https://schema.org' ,
2323 '@type' : 'Thing' ,
2424 name : dog . name ,
25- description : `A ${ dog . breed } dog` ,
25+ description : `A ${ dog . breed } dog: not linked to the page ` ,
2626 } ) ) ;
2727 } ) ;
2828
2929 return {
30- data : {
31- dog,
32- } ,
30+ dog,
3331 } ;
3432} ;
Original file line number Diff line number Diff line change 1- import NuxtJsonld from 'nuxt-jsonld' ;
2-
31export default defineNuxtConfig ( {
4- modules : [ NuxtJsonld ] ,
2+ modules : [ 'nuxt-jsonld' ] ,
53 css : [ '@/css/index.css' ] ,
6- devtools : true ,
4+ compatibilityDate : '2024-12-11' ,
75} ) ;
Original file line number Diff line number Diff line change 11<template >
22 <div >
33 <h1 >Composable Options</h1 >
4+ <nuxt-link to =" /" > Back to list </nuxt-link >
45 </div >
56</template >
67
7- <script lang="ts">
8- export default defineComponent ({
9- setup() {
10- useJsonld (
11- () => {
12- return {
13- ' @context' : ' https://schema.org' ,
14- ' @type' : ' WebSite' ,
15- name: ' nuxt-jsonld composable options' ,
16- };
17- },
18- {
19- tagPosition: ' bodyClose' ,
20- }
21- );
8+ <script lang="ts" setup>
9+ useJsonld (
10+ () => {
11+ return {
12+ ' @context' : ' https://schema.org' ,
13+ ' @type' : ' WebSite' ,
14+ name: ' nuxt-jsonld composable options' ,
15+ };
2216 },
23- });
17+ {
18+ tagPosition: ' bodyClose' ,
19+ }
20+ );
2421 </script >
Original file line number Diff line number Diff line change 22 <div >
33 <h1 >Context example</h1 >
44 <div >
5- <pre >{{ JSON.stringify(data. dog, null, 4) }}</pre >
5+ <pre >{{ JSON.stringify(dog, null, 4) }}</pre >
66 <nuxt-link to =" /" > Back to list </nuxt-link >
77 </div >
88 </div >
99</template >
1010
1111<script lang="ts" setup>
1212import { useDog } from ' @/composables/dog' ;
13- const { data } = await useDog ();
13+ const { dog } = await useDog ();
14+ useJsonld (() => ({
15+ ' @context' : ' https://schema.org' ,
16+ ' @type' : ' Thing' ,
17+ name: dog .name ,
18+ description: ` A ${dog .breed } dog: linked to this page ` ,
19+ }));
1420 </script >
1521
1622<style scoped>
Original file line number Diff line number Diff line change 88 {{ p.name }}
99 </nuxt-link >
1010 </li >
11- <li ><nuxt-link :to =" { name: 'static' }" >Static JSON</nuxt-link ></li >
1211 <li ><nuxt-link :to =" { name: 'option' }" >Options API</nuxt-link ></li >
1312 <li ><nuxt-link :to =" { name: 'composable-options' }" >Composable API Options</nuxt-link ></li >
1413 <li ><nuxt-link :to =" { name: 'context' }" >Context</nuxt-link ></li >
1514 </ul >
1615 </div >
1716</template >
1817
19- <script lang="ts">
20- import type { WithContext , ItemList } from ' schema-dts' ;
18+ <script lang="ts" setup>
19+ useHead ({
20+ title: ' Product List' ,
21+ });
2122
22- export default defineComponent ({
23- setup() {
24- useHead ({
25- title: ' Product List ' ,
26- }) ;
23+ const products = [
24+ { name: ' Foo ' , id: 1 },
25+ { name: ' Bar ' , id: 2 },
26+ { name: ' Baz ' , id: 3 } ,
27+ ] ;
2728
28- return {
29- products: [
30- { name: ' Foo' , id: 1 },
31- { name: ' Bar' , id: 2 },
32- { name: ' Baz' , id: 3 },
33- ],
34- };
35- },
36- jsonld(): WithContext <ItemList > {
37- return {
38- ' @context' : ' https://schema.org' ,
39- ' @type' : ' ItemList' ,
40- itemListElement: this .products .map ((p ) => ({
41- ' @type' : ' ListItem' ,
42- position: p .id ,
43- item: {
44- ' @type' : ' Product' ,
45- name: p .name ,
46- },
47- })),
48- };
49- },
29+ useJsonld ({
30+ ' @context' : ' https://schema.org' ,
31+ ' @type' : ' ItemList' ,
32+ itemListElement: products .map ((p ) => ({
33+ ' @type' : ' ListItem' ,
34+ position: p .id ,
35+ item: {
36+ ' @type' : ' Product' ,
37+ name: p .name ,
38+ },
39+ })),
5040});
5141 </script >
Original file line number Diff line number Diff line change 1010 </div >
1111</template >
1212
13- <script lang="ts">
14- export default defineComponent ({
15- setup() {
16- const { params } = useRoute ();
17- const count = ref <number >(0 );
18- const updateCount = () => {
19- count .value ++ ;
20- };
21- const product = reactive ({
22- name: params .id ,
23- description: ` This is a sample ${params .id } product. ` ,
24- count ,
25- });
26-
27- useHead ({
28- title: ` Product: ${product .name } ` ,
29- });
13+ <script lang="ts" setup>
14+ const { params } = useRoute ();
15+ const count = ref <number >(0 );
16+ const updateCount = () => {
17+ count .value ++ ;
18+ };
19+ const product = reactive ({
20+ name: params .id ,
21+ description: ` This is a sample ${params .id } product. ` ,
22+ count ,
23+ });
3024
31- useJsonld (() => {
32- if (! product .count ) {
33- return null ;
34- }
35- return {
36- ' @context' : ' https://schema.org' ,
37- ' @type' : ' Product' ,
38- ... product ,
39- };
40- });
25+ useHead ({
26+ title: ` Product: ${product .name } ` ,
27+ });
4128
42- return {
43- updateCount ,
44- product ,
45- };
46- },
29+ useJsonld (() => {
30+ if (! product .count ) {
31+ return null ;
32+ }
33+ return {
34+ ' @context' : ' https://schema.org' ,
35+ ' @type' : ' Product' ,
36+ ... product ,
37+ };
4738});
4839 </script >
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments