Nuxt3入门:介绍、项目安装和了解视图(第一节)

你好同学,我是沐爸,欢迎点赞、收藏、评论和关注。

有对 Nuxt3.0 感兴趣的小伙伴吗?一起来了解下

一、介绍

Nuxt is an open source framework that makes web development intuitive and powerful.
Create performant and production-grade full-stack web apps and websites with confidence.

Nuxt 是一个开源框架,使 Web 开发变得直观而强大。自信地创建高性能和生产级全栈 Web 应用程序和网站。

特性

  • 基于文件的路由系统
  • 组件自动导入功能
  • SEO & Meta 标签
  • 强大的中间件系统
  • 内置状态管理
  • 内置错误处理和日志记录
  • 灵活的渲染模式
  • TypeSript 支持

二、安装

先决条件

  • Node.js - V18.0.0 或更高版本
  • 编辑器 - 推荐使用带有 Vue 扩展(Vetur)的 VSCode

使用以下命令创建项目

npx nuxi@latest init <project-name>

运行后会提示两个选项:选择包管理工具,是否初始化代码仓库。

切换目录

cd <project-name>

启动应用

npm run dev -o

运行成功后,浏览器窗口将自动打开 http://localhost:3000。
在这里插入图片描述

三、视图

Nuxt 提供了多个组件层来实现应用程序的用户界面。

app.vue

默认情况下,Nuxt 会将此文件视为入口点,并为应用程序的每个路由呈现其内容。

app.vue 文件

<template>
  <div>
    <h1>Welcome to the homepage</h1>
  </div>
</template>

组件 components

大多数组件都是用户界面的可重用部分,例如按钮和菜单。在 Nuxt 中,您可以在 components/ 目录中,它们将自动在您的应用程序中可用,而无需显式导入它们。

app.vue

<template>
  <div>
    <h1>Welcome to the homepage</h1>
    <AppAlert> This is an auto-imported component. </AppAlert>
  </div>
</template>

AppAlert.vue

<template>
  <span>
    <slot />
  </span>
</template>

页面 pages

页面是应用程序的每个路由的入口点。在 Nuxt 中,页面位于pages/目录中。要使用页面,需要将<NuxtPage />组件放置在app.vue中。

app.vue

<template>
  <div>
    <NuxtPage />
  </div>
</template>

pages/index.vue

<template>
  <div>
    <h1>Welcome to the homepage</h1>
    <AppAlert> This is an auto-imported component </AppAlert>
  </div>
</template>

pages/about.vue

<template>
  <section>
    <p>This page will be displayed at the /about route.</p>
  </section>
</template>

布局 layouts

布局是页面组件的包装器,用于共享页面的公共部分,例如页眉和页脚。可以在layouts/目录中创建布局,default.vue表示默认布局。

app.vue

<template>
  <div>
    <NuxtLayout>
      <NuxtPage />
    </NuxtLayout>
  </div>
</template>

layouts/default.vue

<template>
  <div>
    <AppHeader />
    <slot />
    <AppFooter />
  </div>
</template>

components/AppHeader.vue

<template>
  <div style="background: #ddd;">This is Header.</div>
</template>

components/AppFooter.vue

<template>
  <div class="footer">This is Footer.</div>
</template>

<style scoped>
.footer {
  background: #ddd;
  width: 100%;
  position: fixed;
  bottom: 0;
}
</style>

pages/index.vue

<template>
  <div>
    <h1>Welcome to the homepage</h1>
    <AppAlert> This is an auto-imported component </AppAlert>
  </div>
</template>

在这里插入图片描述
好了,分享结束,谢谢点赞,下期再见!

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沐爸muba

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值