01
快速上手
页面
pages/ 目录下的文件路由。
nuxtjs
<!-- pages/index.vue -->
<template>
<h1>Hello</h1>
</template>数据获取
useFetch 支持数据获取并兼容 SSR。
nuxtjs
<script setup>
const { data } = await useFetch("/api/data");
</script>
<template>
<div>{{ data }}</div>
</template>布局
布局用插槽承载页面内容。
nuxtjs
<!-- layouts/default.vue -->
<template>
<header>Nav</header>
<main><slot /></main>
</template>02
核心特性
动态路由
用 [slug] 表示动态路由。
nuxtjs
<!-- pages/blog/[slug].vue -->
<script setup>
const route = useRoute();
</script>
<template>
<h1>{{ route.params.slug }}</h1>
</template>组合式函数
组合式函数自动导入。
nuxtjs
// composables/useCounter.ts
export function useCounter() {
const count = ref(0);
const inc = () => count.value++;
return { count, inc };
}中间件
路由中间件。
nuxtjs
// middleware/auth.ts
export default defineNuxtRouteMiddleware((to) => {
if (!isAuthenticated()) return navigateTo("/login");
});这篇内容对您有帮助吗?