Code Monkey home page Code Monkey logo

init_nuxt's Introduction

learn Nuxt

路由 Routing

https://nuxt.com/docs/getting-started/routing

  1. 配置路由
  • 在 pages 文件夹下创建对应的文件夹和文件
    • nuxt 会自动创建路由
  • 也可以在 nuxt.config.js 中配置路由
  1. 路由导航跳转
  • <nuxt-link to="/about">About</nuxt-link>
    • 使用 nuxt-link 组件来实现路由跳转,不要使用 a 标签
    • 使用 nuxt-link 进行调整不会重新加载页面,只会加载需要的组件 因为 nuxt 是一个 SPA 应用,会在客户端使用 js 来处理路由跳转
    • 如果使用 a 标签,会导致页面重新加载,这样就不是 SPA 应用了
  1. 动态路由
  • 创建文件名 为 [id].vue 的文件
  • 在组件中使用 <nuxt-link to="/user/1">User</nuxt-link> 进行跳转
  • [id].vue 组件中使用
    • const route = useRoute()
    • console.log(route.params.id) // 1
  1. 自定义路由
  • 创建文件 app/router.options.ts
import { RouterConfig } from "nuxt/schema"

const customRoutes = [
  {
    path: "/users",
    name: "users",
    component: () => import("@/pages/users/index.vue"),
  },
  {
    path: "/users/create-or-edit",
    name: "users-create-or-edit",
    component: () => import("@/pages/users/create-or-edit.vue"),
  },
]

export default <RouterConfig>{
  routes: (_routes) => _routes.concat(customRoutes),
}
  • 这是指定路由器的推荐方法,因为它允许您使用 Vue Router 的所有功能,例如路由参数,嵌套路由,命名视图等。
  • 使用路由器配置会覆盖 Nuxt.js 自动生成的路由配置。
  • 如果返回 null 或 undefined,Nuxt.js 将使用自动生成的路由配置。

组件 Components

https://nuxt.com/docs/guide/directory-structure/components#components-directory

  1. 组件的创建
  • 在 components 文件夹下创建组件
  • 在页面中使用 <component-name></component-name> 使用组件
  • 在 components 文件夹下的组件会被自动注册为全局组件
  • 用法和 vue 一样
  1. 组件的传值

todo...

布局 Layouts

https://nuxt.com/docs/guide/directory-structure/layouts

  • 在 layouts 文件夹下创建布局文件
  • 在 app.vue 是中使用
    • <template>
        <NuxtLayout>
          <NuxtPage />
        </NuxtLayout>
      </template>

如果只有一个布局,可以直接在 app.vue 中写,不需要在 layouts 文件夹下创建布局文件

SEO

https://nuxt.com/docs/getting-started/seo-meta

  • nuxt.config.js 中配置
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  app: {
    head: {
      title: "Evan_Sky",
      meta: [{ name: "description", content: "My amazing site." }],
    },
  },
})
  • 在 vue 组件中配置
<script setup>
useHead({
  title: 'Evan_Sky',
  meta: [
    { name: 'description', content: 'My amazing site.' }
  ]
})
</script>
  • 预定义组件
<script setup>
const title = ref("Hello World")
</script>

<template>
  <div>
    <Head>
      <Title>{{ title }}</Title>
      <Meta name="description" :content="title" />
      <Style type="text/css" children="body { background-color: green; }" />
    </Head>

    <h1>{{ title }}</h1>
  </div>
</template>
  • ......
  • 更多用法参考官网

静态资源 Assets

https://nuxt.com/docs/getting-started/assets

  • public 文件夹下的文件会被映射到根路径 /

    • 例如 引用 public/img 下的图片,可以使用 /img/xxx.png 进行引用
    • <template>
        <img src="/img/nuxt.png" alt="Discover Nuxt 3" />
      </template>
    • public 下的文件不会被 webpack 处理
  • assets 文件夹下的文件会被 webpack 处理

    • 例如 引用 assets/img 下的图片,可以使用 ~/assets/img/xxx.png 进行引用
    • <template>
        <img src="~/assets/img/nuxt.png" alt="Discover Nuxt 3" />
      </template>
    • assets 下的文件会被 webpack 处理

配置 Configuration

https://nuxt.com/docs/getting-started/configuration

  • runtimeConfig

    • 在 nuxt.config.js 中配置

      • export default defineNuxtConfig({
          runtimeConfig: {
            apiSecret: "123", // 仅在服务器端可用
            public: {
              // 在客户端和服务器端都可用
              baseURL: "http://localhost:3000",
            },
          },
        })
    • 在 vue 组件中使用

      • <script setup>const runtimeConfig = useRuntimeConfig()</script>
  • env 文件

    • 在根目录下创建 .env 文件
    • NUXT_PUBLIC_BASE_URL = "https://bilibili.com"
      // 会覆盖 runtimeConfig 中的配置

获取数据 Data fetching

https://nuxt.com/docs/getting-started/data-fetching

useFetch 接收 URL 并获取该数据,而 useAsyncData 可能具有更复杂的逻辑。 useFetch(url) 几乎等同于 useAsyncData(url, () => $fetch(url)) - 它是最常见用例的开发人员体验糖。

  • 默认情况下,客户端页面会在数据请求完毕后再进行渲染。
  • 如果添加了 lazy 属性,客户端页面会在数据请求之前进行渲染,然后在数据请求完毕后再次渲染。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.