docs(examples): normalize examples using tips (#3852)

This commit is contained in:
Clément Ollivier 2022-03-23 09:44:36 +01:00 committed by GitHub
parent e16998126d
commit 797da68979
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 232 additions and 77 deletions

View File

@ -5,7 +5,7 @@ const bar = getBar()
</script> </script>
<template> <template>
<NuxtExampleLayout example="config-extends"> <NuxtExampleLayout show-tips example="config-extends">
theme runtimeConfig theme runtimeConfig
<pre>{{ JSON.stringify(themeConfig, null, 2) }}</pre> <pre>{{ JSON.stringify(themeConfig, null, 2) }}</pre>
<BaseButton>Base Button</BaseButton> <BaseButton>Base Button</BaseButton>
@ -15,6 +15,11 @@ const bar = getBar()
{{ foo }} {{ bar }} {{ foo }} {{ bar }}
<br> <br>
{{ $myPlugin() }} {{ $myPlugin() }}
<template #tips>
<p>
This example shows how to use the <code>extends</code> key in <code>nuxt.config.ts</code> to use the <code>base/</code> directory as a base Nuxt application, and use its components, composable or config and override them if necessary.
</p>
</template>
</NuxtExampleLayout> </NuxtExampleLayout>
</template> </template>

View File

@ -6,7 +6,7 @@ const date = useLocaleDate(new Date('2016-10-26') /* NUXT_BIRTHDAY */)
</script> </script>
<template> <template>
<NuxtExampleLayout :show-tips="true" example="locale"> <NuxtExampleLayout show-tips example="locale">
<h1 class="text-xl opacity-50"> <h1 class="text-xl opacity-50">
Nuxt birthday Nuxt birthday
</h1> </h1>
@ -22,9 +22,12 @@ const date = useLocaleDate(new Date('2016-10-26') /* NUXT_BIRTHDAY */)
</select> </select>
<template #tips> <template #tips>
<div> <p>
This example shows how to define a locale composable to handle the application's locale, both server and client-side.
</p>
<p>
You can right click to "View Page Source" and see that Nuxt renders the correct date in SSR based on visitor's locale. You can right click to "View Page Source" and see that Nuxt renders the correct date in SSR based on visitor's locale.
</div> </p>
</template> </template>
</NuxtExampleLayout> </NuxtExampleLayout>
</template> </template>

View File

@ -1,5 +1,10 @@
<template> <template>
<NuxtExampleLayout example="module-extend-pages"> <NuxtExampleLayout show-tips example="module-extend-pages">
<slot /> <slot />
<template #tips>
<p>
This example defines a new <code>test</code> page using <code>extendPages</code> within a module.
</p>
</template>
</NuxtExampleLayout> </NuxtExampleLayout>
</template> </template>

View File

@ -1,5 +1,10 @@
<template> <template>
<div> <div>
Test page added by module <p>
Go to <NuxtLink to="/">
Homepage
</NuxtLink>
</p>
<p>Test page added by module</p>
</div> </div>
</template> </template>

View File

@ -1,6 +1,12 @@
<template> <template>
<NuxtExampleLayout :show-tips="true" example="nuxt-link" class="example"> <NuxtExampleLayout show-tips example="nuxt-link" class="example">
<NuxtPage /> <NuxtPage />
<template #tips>
<p>
This example shows different ways to use <code>Nuxtlink</code>.
<code>components/myNuxtLink.js</code> defines a custom <code>NuxtLink</code>.
</p>
</template>
</NuxtExampleLayout> </NuxtExampleLayout>
</template> </template>

View File

@ -16,12 +16,17 @@ const { data, refresh, pending } = await useAsyncData('/api/hello', () => $fetch
</div> </div>
<template #tips> <template #tips>
<div> <div>
Nuxt will automatically read files in the <p>
<a href="https://v3.nuxtjs.org/docs/directory-structure/server#api-routes" target="_blank"> This example shows how to use <code>useAsyncData</code> to fetch data from an API endpoint.
<code>~/server/api</code> directory </p>
</a> <p>
to create API endpoints. Learn more about Nuxt will automatically read files in the
<a href="https://v3.nuxtjs.org/docs/usage/data-fetching" target="_blank">data fetching</a> <a href="https://v3.nuxtjs.org/docs/directory-structure/server#api-routes" target="_blank">
<code>~/server/api</code> directory
</a>
to create API endpoints. Learn more about
<a href="https://v3.nuxtjs.org/docs/usage/data-fetching" target="_blank">data fetching</a>
</p>
</div> </div>
</template> </template>
</NuxtExampleLayout> </NuxtExampleLayout>

View File

@ -4,7 +4,7 @@ const { data } = await useFetch(() => `/api/hello/${count.value}`, { params: { t
</script> </script>
<template> <template>
<NuxtExampleLayout example="use-fetch" :show-tips="true"> <NuxtExampleLayout example="use-fetch" show-tips>
<div> <div>
Fetch result: Fetch result:
<pre class="text-left"><code>{{ data }}</code></pre> <pre class="text-left"><code>{{ data }}</code></pre>
@ -14,12 +14,17 @@ const { data } = await useFetch(() => `/api/hello/${count.value}`, { params: { t
</div> </div>
<template #tips> <template #tips>
<div> <div>
Nuxt will automatically read in files in the <p>
<a href="https://v3.nuxtjs.org/docs/directory-structure/server#api-routes" target="_blank"> This example shows how to use <code>useFetch</code> to fetch data from an API endpoint.
<code>~/server/api</code> directory </p>
</a> <p>
to create API endpoints. Learn more about Nuxt will automatically read in files in the
<a href="https://v3.nuxtjs.org/docs/usage/data-fetching" target="_blank">data fetching</a>. <a href="https://v3.nuxtjs.org/docs/directory-structure/server#api-routes" target="_blank">
<code>~/server/api</code> directory
</a>
to create API endpoints. Learn more about
<a href="https://v3.nuxtjs.org/docs/usage/data-fetching" target="_blank">data fetching</a>.
</p>
</div> </div>
</template> </template>
</NuxtExampleLayout> </NuxtExampleLayout>

View File

@ -1,5 +1,5 @@
<template> <template>
<NuxtExampleLayout example="use-meta" :show-tips="true"> <NuxtExampleLayout example="use-meta" show-tips>
<div <div
class="bg-gray-400/10 border-2 border-dashed border-gray-400/50 rounded-xl py-8 px-2 op-80" class="bg-gray-400/10 border-2 border-dashed border-gray-400/50 rounded-xl py-8 px-2 op-80"
> >
@ -23,11 +23,16 @@
<template #tips> <template #tips>
<div class="flex-auto"> <div class="flex-auto">
Learn more about <p>
<NuxtLink to="https://v3.nuxtjs.org/docs/usage/meta-tags" target="_blank"> This example shows how to use <code>useMeta</code> and Nuxt built-in components to bind meta data to the <code>head</code> of the page.
Meta tags </p>
</NuxtLink>. <p>
Open in editor to see the source code 👉 Learn more about
<NuxtLink to="https://v3.nuxtjs.org/docs/usage/meta-tags" target="_blank">
Meta tags
</NuxtLink>.
Open in editor to see the source code 👉
</p>
</div> </div>
</template> </template>
</NuxtExampleLayout> </NuxtExampleLayout>

View File

@ -3,7 +3,7 @@ const counter = useState('counter', () => Math.round(Math.random() * 1000))
</script> </script>
<template> <template>
<NuxtExampleLayout :show-tips="true" example="use-state"> <NuxtExampleLayout show-tips example="use-state">
<div>Counter: {{ counter }}</div> <div>Counter: {{ counter }}</div>
<div> <div>
<NButton class="font-mono" @click="counter++"> <NButton class="font-mono" @click="counter++">

View File

@ -1,5 +1,5 @@
<template> <template>
<NuxtExampleLayout :show-tips="true" example="with-components"> <NuxtExampleLayout show-tips example="with-components">
<h1 class="text-xl opacity-50"> <h1 class="text-xl opacity-50">
Auto Imported Components Auto Imported Components
</h1> </h1>
@ -8,15 +8,19 @@
<HelloWorld class="text-2xl" /> <HelloWorld class="text-2xl" />
<Nuxt3 class="text-2xl" /> <Nuxt3 class="text-2xl" />
<ParentFolderHello class="mt-6" /> <ParentFolderHello class="mt-6" />
<NuxtWithPrefix class="mt-6" />
</div> </div>
<template #tips> <template #tips>
<div> <div>
Components under <code>components/</code> folder are auto imported and can be directly used in your templates. <p>Components under <code>components/</code> directory are auto imported and can be directly used in your templates.</p>
Read more about <p>Other directories can be configured to support components auto-imports</p>
<NuxtLink to="https://v3.nuxtjs.org/docs/directory-structure/components" target="_blank"> <p>
the components directory. Read more about
</NuxtLink> <NuxtLink to="https://v3.nuxtjs.org/docs/directory-structure/components" target="_blank">
the components directory.
</NuxtLink>
</p>
</div> </div>
</template> </template>
</NuxtExampleLayout> </NuxtExampleLayout>

View File

@ -1,5 +1,7 @@
<template> <template>
<div> <NCard class="flex flex-col gap-1 p-4">
Awesome Component <code>nuxt.config.ts</code> can specify other directories like
</div> <code class="op50">`other-components-folder/`</code>
to import components from and specify prefixes.
</NCard>
</template> </template>

View File

@ -1,11 +1,32 @@
<template> <template>
<div> <NuxtExampleLayout show-tips example="with-composables">
{{ a }} <p>
{{ b }} Named export <code>useA</code> : {{ a }}
{{ c }} </p>
{{ d }} <p>
{{ foo }} Named export <code>useB</code> : {{ b }}
</div> </p>
<p>
Named export <code>useC</code> : {{ c }}
</p>
<p>
Named export <code>useD</code> : {{ d }}
</p>
<p>
Default export <code>useFoo</code> : {{ foo }}
</p>
<template #tips>
<div>
<p>
This example shows how to use the <code>composables/</code> directory to auto import composables.
</p>
<p>
If a default export is provided, the name of the composable will be mapped to the name of the file.
Named exports can be used as-is.
</p>
</div>
</template>
</NuxtExampleLayout>
</template> </template>
<script setup> <script setup>

View File

@ -15,7 +15,7 @@ function triggerError () {
</script> </script>
<template> <template>
<NuxtExampleLayout example="with-errors"> <NuxtExampleLayout show-tips example="with-errors">
<template #nav> <template #nav>
<nav class="flex align-center gap-4 p-4"> <nav class="flex align-center gap-4 p-4">
<NuxtLink to="/" class="n-link-base"> <NuxtLink to="/" class="n-link-base">
@ -39,12 +39,18 @@ function triggerError () {
</nav> </nav>
</template> </template>
<FaultyComponent />
<template #footer> <template #footer>
<div class="text-center p-4 op-50"> <div class="text-center p-4 op-50">
Current route: <code>{{ route.path }}</code> Current route: <code>{{ route.path }}</code>
</div> </div>
</template> </template>
<template #tips>
<div>
<p>This example shows how to handle errors in different contexts: pages, plugins, components and middleware.</p>
<a href="https://v3.nuxtjs.org/docs/usage/error-handling" target="_blank">
Read more about error handling
</a>
</div>
</template>
</NuxtExampleLayout> </NuxtExampleLayout>
</template> </template>

View File

@ -3,7 +3,7 @@
<strong>Custom layout</strong> <strong>Custom layout</strong>
Header slot: Header slot:
<slot name="header"> <slot name="header">
Default slot content Default slot content for <code>Custom</code> layout
</slot> </slot>
<slot /> <slot />
</div> </div>

View File

@ -0,0 +1,7 @@
import { defineNuxtConfig } from 'nuxt3'
export default defineNuxtConfig({
modules: [
'@nuxt/ui'
]
})

View File

@ -1,6 +1,7 @@
<template> <template>
<div> <div class="p-4">
Same layout as home Custom layout defined with <code>definePageMeta</code>
<br>
<NuxtLink to="/"> <NuxtLink to="/">
Back to home Back to home
</NuxtLink> </NuxtLink>

View File

@ -0,0 +1,9 @@
<template>
<div>
<p>Content inside <code>default</code> layout</p>
<br>
<NuxtLink to="/">
Back to home
</NuxtLink>
</div>
</template>

View File

@ -1,9 +1,11 @@
<template> <template>
<div> <div class="p-4">
Manual control Custom layout defined dynamically with the <code>NuxtLayout</code> component
<br>
<NuxtLayout :name="layout"> <NuxtLayout :name="layout">
Default slot Default slot
<button @click="layout ? layout = null : layout = 'custom'"> <br>
<button class="border p-1 rounded" @click="layout ? layout = null : layout = 'custom'">
Switch layout Switch layout
</button> </button>
@ -11,6 +13,7 @@
Header slot Header slot
</template> </template>
</NuxtLayout> </NuxtLayout>
<br>
<NuxtLink to="/"> <NuxtLink to="/">
Back to home Back to home
</NuxtLink> </NuxtLink>

View File

@ -1,18 +1,26 @@
<template> <template>
<div> <NuxtExampleLayout show-tips example="with-layouts">
Welcome to Nuxt Layouts 👋 <template #nav>
<nav class="flex align-center gap-4 p-4">
<NuxtLink to="/default">
Default layout
</NuxtLink>
<NuxtLink to="/custom">
Custom layout
</NuxtLink>
<NuxtLink to="/dynamic">
Dynamic layout
</NuxtLink>
</nav>
</template>
<NuxtLink to="/manual"> <template #tips>
Manual layout <div>
</NuxtLink> <p>This example shows how to define default and custom layouts.</p>
<NuxtLink to="/same"> <a href="https://v3.nuxtjs.org/docs/directory-structure/layouts" target="_blank">
Same layout Read more about layouts
</NuxtLink> </a>
</div> </div>
</template>
</NuxtExampleLayout>
</template> </template>
<script setup>
definePageMeta({
layout: 'custom'
})
</script>

View File

@ -3,7 +3,7 @@ const route = useRoute()
</script> </script>
<template> <template>
<NuxtExampleLayout example="with-middleware"> <NuxtExampleLayout show-tips example="with-middleware">
<NuxtPage /> <NuxtPage />
<template #nav> <template #nav>
@ -25,5 +25,13 @@ const route = useRoute()
Current route: <code>{{ route.path }}</code> Current route: <code>{{ route.path }}</code>
</div> </div>
</template> </template>
<template #tips>
<div>
<p>This example shows how to add route middleware with the <code>middleware/</code> directory or with a plugin, and how to use them globally or per page.</p>
<a href="https://v3.nuxtjs.org/docs/directory-structure/middleware" target="_blank">
Read more about middleware
</a>
</div>
</template>
</NuxtExampleLayout> </NuxtExampleLayout>
</template> </template>

View File

@ -3,7 +3,7 @@ const route = useRoute()
</script> </script>
<template> <template>
<NuxtExampleLayout example="with-pages"> <NuxtExampleLayout show-tips example="with-pages">
<NuxtPage /> <NuxtPage />
<template #nav> <template #nav>
@ -34,5 +34,14 @@ const route = useRoute()
Current route: <code>{{ route.path }}</code> Current route: <code>{{ route.path }}</code>
</div> </div>
</template> </template>
<template #tips>
<div>
<p>This example shows how to use the <code>pages/</code> directory.</p>
<a href="https://v3.nuxtjs.org/docs/directory-structure/pages" target="_blank">
Read more about pages
</a>
</div>
</template>
</NuxtExampleLayout> </NuxtExampleLayout>
</template> </template>

View File

@ -1,5 +1,14 @@
<template> <template>
<NuxtExampleLayout example="with-plugins" show-tips> <NuxtExampleLayout example="with-plugins" show-tips>
<div>{{ $myPlugin() }}</div> <div>{{ $myPlugin() }}</div>
<template #tips>
<div>
<p>This example shows how to use the <code>plugins/</code> directory to auto-register plugins.</p>
<a href="https://v3.nuxtjs.org/docs/directory-structure/plugins" target="_blank">
Read more about plugins
</a>
</div>
</template>
</NuxtExampleLayout> </NuxtExampleLayout>
</template> </template>

View File

@ -12,7 +12,7 @@ function dec () {
</script> </script>
<template> <template>
<NuxtExampleLayout :show-tips="true" example="with-reactivity-transform"> <NuxtExampleLayout show-tips example="with-reactivity-transform">
<div> <div>
<Label :count="count" /> <Label :count="count" />
<div class="flex gap-1 justify-center"> <div class="flex gap-1 justify-center">
@ -27,10 +27,15 @@ function dec () {
<template #tips> <template #tips>
<div class="flex-auto"> <div class="flex-auto">
Read the documentation about <p>
<NLink href="https://vuejs.org/guide/extras/reactivity-transform.html" target="_blank"> This example demonstrates the support of Reactivity transforms in Nuxt 3.
Reactivity Transform. </p>
</NLink> <p>
Read the documentation about
<a href="https://vuejs.org/guide/extras/reactivity-transform.html" target="_blank">
Reactivity Transform.
</a>
</p>
</div> </div>
</template> </template>
</NuxtExampleLayout> </NuxtExampleLayout>

View File

@ -4,7 +4,7 @@ const timer = useState('timer', () => 0)
</script> </script>
<template> <template>
<NuxtExampleLayout example="with-universal-router"> <NuxtExampleLayout show-tips example="with-universal-router">
A page... A page...
<br> <br>
@ -31,5 +31,13 @@ const timer = useState('timer', () => 0)
Current route: <code>{{ route.path }}</code> Current route: <code>{{ route.path }}</code>
</div> </div>
</template> </template>
<template #tips>
<div>
<p>
This example demonstrates Nuxt universal routing utilities without depending on `pages/` and `vue-router`.
</p>
</div>
</template>
</NuxtExampleLayout> </NuxtExampleLayout>
</template> </template>

View File

@ -8,7 +8,23 @@ const { data } = await useAsyncData('sum',
</script> </script>
<template> <template>
<NuxtExampleLayout example="with-wasm"> <NuxtExampleLayout show-tips example="with-wasm">
{{ a }} + {{ b }} = {{ data.sum }} <p>
<code>a = 100</code>
</p>
<p>
<code>b = 250</code>
</p>
<p>
Computation performed server-side with WASM :
<br>
<code>{{ a }} + {{ b }} = {{ data.sum }}</code>
</p>
<template #tips>
<p>
This example demonstrates the server-side support of WebAssembly in Nuxt 3.
</p>
</template>
</NuxtExampleLayout> </NuxtExampleLayout>
</template> </template>