初步学习Nuxt3

Nuxt3 用于制作ssr 网页 支持vue3 所有的语法,并且支持了TypeScript, vite+vue3+composition api + ts。SPA单页面应用不能进行SEO优化,SSR应用在服务端进行渲染,渲染完成后返回给客户端,每一个页面有独立的URL对SEO友好。

1.Nuxt3安装

  

初始化函数

  npx nuxi init nuxt3-test

  进入项目

  cd nuxt3-test

  安装依赖包

  npm install

  运行项目

  npm run dev

 

2.Nuxt3基础目录结构

 

  - .nuxt // 自动生成的目录,用于展示结果

  - node_modules // 项目依赖包存放目录

  - .gitignore // Git的配置目录,比如一些文件不用Git管理就可以在这个文件中配置

  - app.vue // 项目入口文件,你可以在这里配置路由的出口

  - nuxt.config.ts // nuxt项目的配置文件 ,这个里边可以配置Nuxt项目的方法面面

  - package-lock.json // 锁定安装时包的版本,以保证其他人在 npm install时和你保持一致

  - package.json // 包的配置文件和项目的启动调式命令配置 - README.md // 项目的说明文件

  - tsconfig.json // TypeScript的配置文件

 

3.Nuxt3约定路由,嵌套路由

 根目录下新建pages 然后文件夹里面新建index.vue

 在根目录下app.vue 文件中使用<Nuxtpage>  路由的出口

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

 在目录下创建demo1.vue 文件  然后就在index.vue 跳转到demo1.vue 使用NuxtLink 标签

<template>
  <div >
    <h1>Index Page</h1>
    <NuxtLink to="/demo1">Demo1.vue</NuxtLink>
  </div>
</template>

 创建一个嵌套路由  

          建立嵌套路由的文件夹(约定大于配置)

         创建和文件夹相同名称的文件(父页面)

    在新建文件夹下任意创建子页面

|--pages
|----parent/
|------child.vue
|----parent.vue

  parent.vue  里面引用子页面

<template>
  <div class="">Parent Page</div>
  <!-- 子页面的出口-->
  <NuxtChild></NuxtChild>
</template>

<script setup>
import {} from "vue";
</script>

<style scoped></style>

 

  

4.Nuxt3动态路由的使用

 但参数传递只要在页面的文件名中用 [ ] 括起来就好了,例如  demo2-[id].vue

-| pages/
---| index.vue
---| demo2-[id].vue

   参数接收使用$route.params.id 的形式

 在开发者中常使用的

<template>
  <div class="">获取的id:{{ id }}</div>
</template>

<script setup>
import { ref } from "vue";
const route = useRoute();
const id = ref(route.params.id);
</script>

<style scoped></style

 多参数的传递和获取

  如果传递的是两个参数,那么就需要建立一个文件夹在文件夹上使用[ ] 来确定参数 

-|  pages/
---| index.vue
---| goods-[name]/
-----| demo2-[id].vue

        页面上获取参数

<template>
  <div class="">获取的id:{{ id }}</div>
  <div class="">获取的name:{{ name }}</div>
</template>

<script setup>
import { ref } from "vue";
const route = useRoute();
const id = ref(route.params.id);
const name = ref(route.params.name);
</script>

<style scoped></style

      页面跳转的时候

  <NuxtLink to="/goods-shangpin/demo2-38">Demo2.vue</NuxtLink>

posted @ 2022-06-24 15:56  柠檬先生  阅读(1990)  评论(0编辑  收藏  举报