This repository was archived by the owner on Dec 5, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +75
-3
lines changed
Expand file tree Collapse file tree 5 files changed +75
-3
lines changed Original file line number Diff line number Diff line change @@ -93,6 +93,23 @@ export default defineComponent({
9393
9494** Note** : ` useFetch ` must be called synchronously within ` setup() ` . Any changes made to component data - that is, to properties _ returned_ from ` setup() ` - will be sent to the client and directly loaded. Other side-effects of ` useFetch ` hook will not be persisted.
9595
96+ ### useHead
97+
98+ ``` ts
99+ import { defineComponent , useHead , computed } from ' nuxt-composition-api'
100+
101+ const { head, useMeta } = useHead ()
102+
103+ export default defineComponent ({
104+ head , // this line is needed!
105+ setup() {
106+ const { title } = useMeta ()
107+
108+ title .value = ' newSetTitle'
109+ },
110+ })
111+ ```
112+
96113### ssrRef
97114
98115When creating composition utility functions, often there will be server-side state that needs to be conveyed to the client.
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ export const meta = require('../package.json')
2828export { useFetch } from './fetch'
2929export { withContext } from './context'
3030export { ssrRef , onServerPrefetch , setSSRContext } from './ssr-ref'
31+ export { useHead } from './meta'
3132
3233export {
3334 ComponentRenderProxy ,
Original file line number Diff line number Diff line change 1+ import { reactive , toRefs } from '@vue/composition-api'
2+ import { MetaInfo } from 'vue-meta'
3+
4+ function createEmptyMeta ( ) : MetaInfo {
5+ return {
6+ title : undefined ,
7+ titleTemplate : undefined ,
8+ htmlAttrs : undefined ,
9+ headAttrs : undefined ,
10+ bodyAttrs : undefined ,
11+
12+ base : undefined ,
13+
14+ meta : [ ] ,
15+ link : [ ] ,
16+ style : [ ] ,
17+ script : [ ] ,
18+ noscript : [ ] ,
19+
20+ changed : undefined ,
21+ afterNavigation : undefined ,
22+ }
23+ }
24+
25+ export function useHead ( init : MetaInfo = { } ) {
26+ const meta = reactive < MetaInfo > ( {
27+ ...createEmptyMeta ( ) ,
28+ ...init ,
29+ } )
30+
31+ return {
32+ head : ( ) => meta ,
33+ useMeta : ( ) => toRefs ( meta ) ,
34+ }
35+ }
Original file line number Diff line number Diff line change 11<template >
22 <div >
33 <div >name-{{ name }}</div >
4- <div v-if =" $fetchState.pending" >
5- loading email
6- </div >
4+ <div v-if =" $fetchState.pending" >loading email</div >
75 <div >email-{{ email }}</div >
86 <div >{{ computedProp }}</div >
97 <div >{{ myFunction() }}</div >
108 <nuxt-link to =" /other" >link forward</nuxt-link >
119 <nuxt-link to =" /ssr-ref" >ssr refs</nuxt-link >
10+ <nuxt-link to =" /meta" >meta</nuxt-link >
1211 <button @click =" $fetch" >Refetch</button >
1312 <child-comp />
1413 </div >
Original file line number Diff line number Diff line change 1+ <template >
2+ <div >
3+ <nuxt-link to =" /" >link back</nuxt-link >
4+ </div >
5+ </template >
6+
7+ <script >
8+ import { defineComponent , useHead , computed } from ' nuxt-composition-api'
9+
10+ const { head , useMeta } = useHead ()
11+
12+ export default defineComponent ({
13+ head,
14+ setup () {
15+ const { title } = useMeta ()
16+
17+ title .value = ' newSetTitle'
18+ },
19+ })
20+ </script >
You can’t perform that action at this time.
0 commit comments