nuxt3

703 阅读3分钟

一、nuxt3作用

Nuxt 基于一个强大的模块化架构,让开发变得更快、更简单。会默认优化应用程序,提高应用的性能。既支持生成静态站点、单页应用程序,也支持服务端渲染。

二、nuxt3新特性

  • 更轻量
  • 更快
  • 整合vite + vue3 + composition api + ts、CLI、DevTools、Nuxt Kit

三、创建nuxt3项目

1.创建项目

使用nuxi来创建nuxt项目

npx nuxi init [project-name]

出现以下提示则为创建成功:

image.png

注意1:可能会因为网络问题连不上GitHub,会出现下图两种报错,换个网络多试几次即可~

image.png

image.png

注意2:node版本推荐14.16.0,其他版本安装完依赖开启本地服务的时候可能会报错~

2.根据提示进入项目,并安装依赖:

cd nuxt-app 
cnpm i
  1. 开启本地服务
npm run dev

页面显示如下则项目创建成功:

image.png

四、项目目录

创建好的项目目录十分简洁:

image.png

打开最眼熟的app.vue文件:

<template>
  <div>
    <NuxtWelcome />
  </div>
</template>

文件中只使用了NuxtWelcome组件,并没有看到引入的代码,查找这个组件,可以发现有个components.d.ts文件,其中引入了一些组件,这些组件便是nuxt中自动引入的组件:

image.png

再通过搜索components.d.ts文件在哪里被使用,发现 ./nuxt/nuxt.d.ts中不仅使用了components.d.ts文件,还使用了其他好几个文件:

image.png

查看.nuxt/imports.d.ts文件发现nuxt3已经引入了辅助函数、Vue Api:

export { useHead, useMeta } from '#head';
export { isVue2, isVue3 } from 'vue-demi';
export { useAsyncData, useLazyAsyncData, refreshNuxtData, defineNuxtComponent, useNuxtApp, defineNuxtPlugin, useRuntimeConfig, useState, useFetch, useLazyFetch, useCookie, useRequestHeaders, useRequestEvent, useRouter, useRoute, useActiveRoute, defineNuxtRouteMiddleware, navigateTo, abortNavigation, addRouteMiddleware, throwError, clearError, useError, defineNuxtLink } from '#app';
export { withCtx, withDirectives, withKeys, withMemo, withModifiers, withScopeId, onActivated, onBeforeMount, onBeforeUnmount, onBeforeUpdate, onDeactivated, onErrorCaptured, onMounted, onRenderTracked, onRenderTriggered, onServerPrefetch, onUnmounted, onUpdated, computed, customRef, isProxy, isReactive, isReadonly, isRef, markRaw, proxyRefs, reactive, readonly, ref, shallowReactive, shallowReadonly, shallowRef, toRaw, toRef, toRefs, triggerRef, unref, watch, watchEffect, isShallow, effect, effectScope, getCurrentScope, onScopeDispose, defineComponent, defineAsyncComponent, resolveComponent, getCurrentInstance, h, inject, nextTick, provide, useAttrs, useCssModule, useCssVars, useSlots, useTransitionState } from 'vue';
export { definePageMeta } from 'E:/study/aa/nuxt-app/node_modules/nuxt/dist/pages/runtime/composables';

总结nuxt3的自动导入:

  • Nuxt自动导入

    • useAsyncData, useLazyAsyncData, refreshNuxtData, defineNuxtComponent, ...等方法
  • Vue 自动导入

    • computed, customRef, isProxy, isReactive, isReadonly, isRef, markRaw, proxyRefs, reactive, readonly, ref等这些 Vue3 的相关方法
  • 基于目录的自动导入

    在项目根目录下直接创建

    • components文件夹,下面的组件(.vue)会被自动导入
    • composables文件夹,下面的可组合组件(.ts)会被自动导入
    • assets文件夹,自动导入静态图片等资源

五、路由

nuxt3的路由是根据pages文件夹自动生成的,不用开发人员专门去配置

1. 静态路由

(1) 在page文件夹下创建组件index.vue

image.png

(2) 在app.vue中添加<NuxtPage />组件,作为置放路由页面的容器; <NuxtLink />组件配置路由路径:

image.png

(3) 要重新启动项目,避免出现页面404的情况

image.png

2. 嵌套路由

(1)在在page文件夹下创建todo文件夹,todo文件夹下再创建todoList.vue文件,自动生成嵌套路由

image.png image.png

3.动态路由

动态路由需要[]方括号来进行包裹参数。举个例子: 创建todo-[group]文件夹,该文件夹下创建[id].vue:

image.png

group 和  id  均为动态路由的参数,那么在  [id].vue页面中,通过 route 的 params 可以获取里面的参数值。

image.png 得到以下结果: image.png

六、组件的自动导入

1. 组件

在根目录创建components文件夹,文件夹下的组件会被自动导入,开发过程中使用组件时不再需要手动使用import来导入。

image.png

app.vue文件中使用<List />组件时,不需要手动导入。

2. 嵌套组件

如果组件嵌套在目录内,可以用驼峰式写法。举个例子: components/todo下有个item组件,则使用时写法为:<TodoItem />