Nuxt3 探索(二)Nuxt3 页面的 n 种写法

本文正在参加「金石计划」

前言

继上一篇Nuxt3 探索(一)使用 Nuxt3 搭建一个项目,我们对于 Nuxt 3 项目有了初步的了解了。

然而,"光说不练假把式"。这篇文章中,我们继续探索如何借助 Nuxt 3 的内置组件和 Nuxt3 页面的 n 种写法,快速地开发所需的页面。

内置组件

总的来说,我们可以借助 Nuxt 3 中内置的组件去开发一个新页面。比如 <NuxtWelcome><NuxtLink><ClientObly><Teleport>等等。

<NuxtWelcome>

之前就接触过了,这是一个模板页面,内容如下:

<NuxtPage>

我们需要在 app.vue主页面中,写入<NuxtPage />,这样才能渲染出 pages 目录中的页面。比如,我们渲染出一个导航条和 <NuxtPage /> 组件。

xml 复制代码
<!-- app.vue -->
<template>
  <div>
    <ul>
      <li v-for="(nav, i) in navArr" :key="i">
        <NuxtLink :to="nav.path">{{nav.text}}</NuxtLink>
      </li>
    </ul>
​
    <NuxtPage />
  </div>
</template>
​
<script setup>
  const navArr = [
    {
      path: '/',
      text: 'Index'
    },
    {
      path: '/home',
      text: 'Home'
    },
    {
      path: '/about',
      text: 'About'
    },
    {
      path: '/bag',
      text: 'Bag'
    },
  ]
</script>

<NuxtLink>

用于定义一个页面跳转的超链接。我们在 to 属性 指定要跳转的页面路由,此外还有 target,rel,noRel 等属性。

Props Value
to(href) 可以是页面链接(如'/about')或者 https链接等。
target 同 a 标签的 target 属性,可以设置 _blank 等。
rel 默认值是 noopener noreferrer
noRel 设置为 true 是,不会添加任何的 rel 属性。
activeClass 跟 Vue Router 的 active-class一致。默认值"router-link-active"
exactActiveClass 跟 Vue Router 的 exact-active-class一致。默认值"router-link-exact-active"

<ClientOnly>

该标签包裹的内容只会在客户端进行渲染。

我们可以使用 <ClientOnly> 来包裹只在客户端渲染的页面内容。

<NuxtLoadingIndicator>

展示路由跳转时的加载进度条。传参如下:

Props Value
color The color of the loading bar. It can be set to false to turn off explicit color styling.
height Height of the loading bar, in pixels (default 3).
duration Duration of the loading bar, in milliseconds (default 2000).
throttle Throttle the appearing and hiding, in milliseconds (default 200).

用法如下:

xml 复制代码
<!-- app.vue or layouts/. -->
<template>
  <NuxtLoadingIndicator />
  <NuxtLayout>
    <NuxtPage />
  </NuxtLayout>
</template>

<Teleport>

可以自定义页面内容(比如弹窗)的挂载 DOM。比如我们可以将弹窗挂载到 body 上:

xml 复制代码
<template>
  <button @click="open = true">
    Open Modal
  </button>
  <Teleport to="body">
    <div v-if="open" class="modal">
      <p>Hello from the modal!</p>
      <button @click="open = false">
        Close
      </button>
    </div>
  </Teleport>
</template>

组件的写法

除了使用内置组件,我们更多的是手写开发一个组件。

Nuxt 3 很灵活,支持以下格式的组件:.vue, .js, .jsx, .mjs, .ts or .tsx

Vue3 <script setup>写法

Nuxt 3 支持 Vue3 语法,我们可以方便地书写 <script setup>,然后直接在页面中引入。此外,其他的 Vue 3 语法也是自然的支持的。

xml 复制代码
<template>
  <div>
    <div>Home page</div>
    <div class="name">Hello {{ name }}</div>
  </div>
</template>
​
<script setup>
const name = "Tom"
</script>
​
<style scoped>
.name{
  color: #0f0;
  margin-top: 20px;
}
</style>

.ts文件写法

使用 .ts 文件定义组件时,我们不需要引入 defineComponent 变量。这里借助的是 vue 的 render 函数去渲染一个组件。一个简单的例子如下:

arduino 复制代码
// https://vuejs.org/guide/extras/render-function.html
export default defineComponent({
  render () {
    return h('div', 'about page')
  }
})

.tsx文件写法

使用 .tsx 文件定义组件时,同理我们不需要引入 defineComponent 变量。

如果熟悉 React 语法的话(其实不熟悉 React 也没问题...),在render() {}中 return 对应的 jsx 语句即可。一个简单的例子如下:

ruby 复制代码
// https://nuxt.com/docs/examples/advanced/jsx
// https://vuejs.org/guide/extras/render-function.html#jsx-tsx
export default defineComponent({
  render () {
    return <div>Bag page</div>
  }
})

后记

这段时间,心血来潮在考虑做些什么、偶尔在深入学习某个知识点、时常在摸鱼 ==!

相关推荐
276695829220 分钟前
dy bd-ticket-guard-client-data bd-ticket-guard-ree-public-key 逆向
前端·javascript·python·abogus·bd-ticket·mstoken·ticket-guard
m0_7263658329 分钟前
哈希分分预测系统 + Python Worker + Web 仪表盘”小系统(PHP + MySQL)
前端·python·哈希算法
WX-bisheyuange32 分钟前
基于SpringBoot的交通管理在线服务系统
前端·javascript·vue.js·毕业设计
Coder_Boy_1 小时前
基于SpringAI的在线考试系统-考试系统DDD(领域驱动设计)实现步骤详解(2)
java·前端·数据库·人工智能·spring boot
GIS之路1 小时前
ArcGIS Pro 实现影像波段合成
前端·python·信息可视化
m0_748254661 小时前
CSS id 和 classid 和 class 选择器
前端·css
光算科技2 小时前
单页应用SEO可行性丨Angular项目的3个索引优化方案
前端·javascript·angular.js
ggaofeng2 小时前
实践NPM打包和使用
前端·npm·node.js
一只小bit2 小时前
Qt MainWindow:主窗口组件的介绍与正确使用
前端·c++·qt
Macbethad2 小时前
半导体EFEM设备TwinCAT程序设计方案
java·前端·网络