Nuxt UI v3 是一个全新的主要版本,从头开始重建,引入了现代架构,显著提高了性能,并增强了开发者体验。此主要版本包含多项重大更改以及强大的新功能和特性。
本指南提供了将应用程序从 v2 迁移到 v3 的分步说明。
Tailwind CSS v4 对其配置方法进行了重大更改。官方的 Tailwind 升级工具将帮助自动化大部分迁移过程。
main.css 文件并将其导入到您的 nuxt.config.ts 文件中。@import "tailwindcss";
export default defineNuxtConfig({
css: ['~/assets/css/main.css']
})
npx @tailwindcss/upgrade
pnpm add @nuxt/ui
yarn add @nuxt/ui
npm install @nuxt/ui
bun add @nuxt/ui
@import "tailwindcss";
@import "@nuxt/ui";
<template>
<UApp>
<NuxtPage />
</UApp>
</template>
现在您已经更新了项目,可以开始迁移代码了。以下是 Nuxt UI v3 中所有重大更改的全面列表。
在 Nuxt UI v2 中,我们有一个混合的设计系统,其中包含 primary、gray、error 别名和所有 Tailwind CSS 颜色。我们已将其替换为带有 7 种颜色别名的适当设计系统:
| 颜色 | 默认值 | 描述 |
|---|---|---|
primary | 绿色 | 主要品牌颜色,用作组件的默认颜色。 |
辅助 | 蓝色 | 补充主色的辅助颜色。 |
成功 | 绿色 | 用于成功状态。 |
信息 | 蓝色 | 用于信息状态。 |
警告 | 黄色 | 用于警告状态。 |
error | 红色 | 用于表单错误验证状态。 |
neutral | 板岩 | 用于背景、文本等的中性颜色。 |
此更改引入了一些您需要注意的重大更改
gray 颜色已重命名为 neutral<template>
- <p class="text-gray-500 dark:text-gray-400" />
+ <p class="text-neutral-500 dark:text-neutral-400" />
</template>
<template>
- <p class="text-gray-500 dark:text-gray-400" />
+ <p class="text-muted" />
- <p class="text-gray-900 dark:text-white" />
+ <p class="text-highlighted" />
</template>
color 属性中的 gray、black 和 white 已被移除,取而代之的是 neutral- <UButton color="black" />
+ <UButton color="neutral" />
- <UButton color="gray" />
+ <UButton color="neutral" variant="subtle" />
- <UButton color="white" />
+ <UButton color="neutral" variant="outline" />
color 属性中使用 Tailwind CSS 颜色,请改用新的别名- <UButton color="red" />
+ <UButton color="error" />
app.config.ts 中的颜色配置已移至 colors 对象中export default defineAppConfig({
ui: {
- primary: 'green',
- gray: 'cool'
+ colors: {
+ primary: 'green',
+ neutral: 'slate'
+ }
}
})
Nuxt UI 组件现在使用 Tailwind Variants API 进行样式设置,这使得您之前使用 app.config.ts 和 ui 属性进行的所有覆盖都已过时。
app.config.ts 以使用新主题覆盖组件export default defineAppConfig({
ui: {
button: {
- font: 'font-bold',
- default: {
- size: 'md',
- color: 'primary'
- }
+ slots: {
+ base: 'font-medium'
+ },
+ defaultVariants: {
+ size: 'md',
+ color: 'primary'
+ }
}
}
})
ui 属性以使用新主题覆盖每个组件的插槽<template>
- <UButton :ui="{ font: 'font-bold' }" />
+ <UButton :ui="{ base: 'font-bold' }" />
</template>
我们重命名了一些 Nuxt UI 组件,以与 Reka UI 命名约定保持一致。
| v2 | v3 |
|---|---|
Divider | Separator |
Dropdown | DropdownMenu |
FormGroup | FormField |
Range | Slider |
Toggle | Switch |
Notification | Toast |
VerticalNavigation | NavigationMenu 且 orientation="vertical" |
HorizontalNavigation | NavigationMenu 且 orientation="horizontal" |
以下是已重命名或移除的 Nuxt UI Pro 组件
| v1 | v3 |
|---|---|
BlogList | 博客文章列表 |
ColorModeToggle | 颜色模式开关 |
DashboardCard | 已移除(请改用 PageCard) |
DashboardLayout | 仪表盘组 |
DashboardModal | 已移除(请改用 Modal) |
DashboardNavbarToggle | 仪表盘侧边栏切换 |
DashboardPage | 已移除 |
DashboardPanelContent | 已移除(请改用 #body 插槽) |
DashboardPanelHandle | 仪表盘调整大小手柄 |
DashboardSection | 已移除(请改用 PageCard) |
DashboardSidebarLinks | 已移除(请改用 NavigationMenu) |
DashboardSlideover | 已移除(请改用 Slideover) |
FooterLinks | 已移除(请改用 NavigationMenu) |
HeaderLinks | 已移除(请改用 NavigationMenu) |
LandingCard | 已移除(请改用 PageCard) |
LandingCTA | 页面 CTA |
LandingFAQ | 已移除(请改用 Accordion) |
LandingGrid | 已移除(请改用 PageGrid) |
LandingHero | 已移除(请改用 PageHero) |
LandingLogos | 页面标志 |
LandingSection | 页面节 |
LandingTestimonial | 已移除(请改用 PageCard) |
NavigationAccordion | 内容导航 |
NavigationLinks | 内容导航 |
NavigationTree | 内容导航 |
PageError | 错误 |
PricingCard | 定价方案 |
PricingGrid | 定价方案列表 |
PricingSwitch | 已移除(请改用 Switch 或 Tabs) |
除了重命名的组件,组件 API 也有很多变化。我们来详细说明其中最重要的几项。
links 和 options 属性已重命名为 items。<template>
- <USelect :options="countries" />
+ <USelect :items="countries" />
- <UHorizontalNavigation :links="links" />
+ <UNavigationMenu :items="links" />
</template>
Breadcrumb、HorizontalNavigation、InputMenu、RadioGroup、Select、SelectMenu 和 VerticalNavigation。click 字段已移除,取而代之的是原生的 Vue onClick 事件。<script setup lang="ts">
const items = [{
label: 'Edit',
- click: () => {
+ onClick: () => {
console.log('Edit')
}
}]
</script>
Toast 组件以及所有具有 items 链接的组件,例如 NavigationMenu、DropdownMenu、CommandPalette 等。Modals、Slideovers 和 Notifications 组件已移除,取而代之的是 App 组件。<template>
+ <UApp>
+ <NuxtPage />
+ </UApp>
- <UModals />
- <USlideovers />
- <UNotifications />
</template>
v-model:open 指令及 default-open 属性现在用于控制可见性。<template>
- <UModal v-model="open" />
+ <UModal v-model:open="open" />
</template>
ContextMenu、Modal 和 Slideover,并可用于控制 InputMenu、Select、SelectMenu 和 Tooltip 的可见性。#content 插槽内部(使用此方法无需使用 v-model:open 指令)。<script setup lang="ts">
- const open = ref(false)
</script>
<template>
- <UButton label="Open" @click="open = true" />
- <UModal v-model="open">
+ <UModal>
+ <UButton label="Open" />
+ <template #content>
<div class="p-4">
<Placeholder class="h-48" />
</div>
+ </template>
</UModal>
</template>
Modal、Popover、Slideover、Tooltip。#content 插槽内部添加了 #header、#body 和 #footer 插槽,与 Card 组件类似。<template>
- <UModal>
+ <UModal title="Title" description="Description">
- <div class="p-4">
+ <template #body>
<Placeholder class="h-48" />
+ </template>
- </div>
</UModal>
</template>
Modal、Slideover。useToast() 组合函数中的 timeout 属性已重命名为 duration。<script setup lang="ts">
const toast = useToast()
- toast.add({ title: 'Invitation sent', timeout: 0 })
+ toast.add({ title: 'Invitation sent', duration: 0 })
</script>
useModal 和 useSlideover 组合函数已被移除,取而代之的是更通用的 useOverlay 组合函数。一些重要的区别
useOverlay 组合函数现在用于创建覆盖实例。modal.close() 或 slideover.close() 关闭,而是会自动关闭:要么是当打开的组件明确触发 close 事件时,要么是当覆盖层自行关闭时(点击背景、按 ESC 键等)。close 事件。<script setup lang="ts">
import { ModalExampleComponent } from '#components'
- const modal = useModal()
+ const overlay = useOverlay()
- modal.open(ModalExampleComponent)
+ const modal = overlay.create(ModalExampleComponent)
</script>
属性现在通过 props 属性传递
<script setup lang="ts">
import { ModalExampleComponent } from '#components'
- const modal = useModal()
+ const overlay = useOverlay()
const count = ref(0)
- modal.open(ModalExampleComponent, {
- count: count.value
- })
+ const modal = overlay.create(ModalExampleComponent, {
+ props: {
+ count: count.value
+ }
+ })
</script>
现在通过 close 事件关闭模态框。modal.open 方法现在返回一个实例,该实例可用于在模态框关闭时等待模态框的结果。
<script setup lang="ts">
import { ModalExampleComponent } from '#components'
- const modal = useModal()
+ const overlay = useOverlay()
+ const modal = overlay.create(ModalExampleComponent)
- function openModal() {
- modal.open(ModalExampleComponent, {
- onSuccess() {
- toast.add({ title: 'Success!' })
- }
- })
- }
+ async function openModal() {
+ const instance = modal.open(ModalExampleComponent, {
+ count: count.value
+ })
+
+ const result = await instance.result
+
+ if (result) {
+ toast.add({ title: 'Success!' })
+ }
+ }
</script>
path 重命名为 name。<script setup lang="ts">
const validate = (state: any): FormError[] => {
const errors = []
if (!state.email) {
errors.push({
- path: 'email',
+ name: 'email',
message: 'Required'
})
}
if (!state.password) {
errors.push({
- path: 'password',
+ name: 'password',
message: 'Required'
})
}
return errors
}
</script>