feat: add <Html> meta component (#1820)

Co-authored-by: Sébastien Chopin <seb@nuxtjs.com>
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
This commit is contained in:
Daniel Roe 2021-11-10 20:25:29 +00:00 committed by GitHub
parent d9c1aa6b02
commit abfbd2f34e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 31 deletions

View File

@ -23,28 +23,9 @@ export default {
}
```
## `head` Component Property
If you would prefer to use the options syntax, you can do exactly the same thing within your component options with an object or function called `head`. For example:
```js
export default {
head: {
script: [
{
async: true,
src: 'https://myscript.com/script.js'
}
]
}
}
```
**Note that `head()` does not have access to the component instance.**
## Meta Components
Nuxt provides `<Title>`, `<Base>`, `<Script>`, `<Style>`, `<Meta>`, `<Link>`, `<Body>` and `<Head>` components so that you can interact directly with your metadata within your component template.
Nuxt provides `<Title>`, `<Base>`, `<Script>`, `<Style>`, `<Meta>`, `<Link>`, `<Body>`, `<Html>` and `<Head>` components so that you can interact directly with your metadata within your component template.
Because these component names match native HTML elements, it is very important that they are capitalized in the template.
@ -56,11 +37,13 @@ For example:
<template>
<div>
Hello World
<Head :lang="dynamic > 50 ? 'en-GB' : 'en-US'">
<Title>{{ dynamic }} title</Title>
<Meta name="description" :content="`My page's ${dynamic} description`" />
<Link rel="preload" href="/test.txt" as="script" />
</Head>
<Html :lang="dynamic > 50 ? 'en-GB' : 'en-US'">
<Head>
<Title>{{ dynamic }} title</Title>
<Meta name="description" :content="`My page's ${dynamic} description`" />
<Link rel="preload" href="/test.txt" as="script" />
</Head>
</Html>
<button class="blue" @click="dynamic = Math.random() * 100">
Click me

View File

@ -2,11 +2,13 @@
<div>
Hello World
<Head :lang="'' + dynamic">
<Title>{{ dynamic }} title</Title>
<Meta name="description" :content="`My page's ${dynamic} description`" />
<Link rel="preload" href="/test.txt" as="script" />
</Head>
<Html :lang="String(dynamic)">
<Head>
<Title>{{ dynamic }} title</Title>
<Meta name="description" :content="`My page's ${dynamic} description`" />
<Link rel="preload" href="/test.txt" as="script" />
</Head>
</Html>
<button class="blue" @click="dynamic = Math.random() * 100">
Clickme

View File

@ -193,13 +193,19 @@ export const Style = defineComponent({
// <head>
export const Head = defineComponent({
name: 'Head',
setup: (_props, ctx) => ctx.slots.default
})
// <html>
export const Html = defineComponent({
name: 'Html',
props: {
...globalProps,
manifest: String,
version: String,
xmlns: String
},
setup: setupForUseMeta(headAttrs => ({ headAttrs }), true)
setup: setupForUseMeta(htmlAttrs => ({ htmlAttrs }), true)
})
// <body>