nuxt3:我们开始吧-开发-配置-部署(二)

简介: nuxt3:我们开始吧-开发-配置-部署(二)

6.6、layouts

6.6.1、理解:布局、页面布局、公共布局、基础布局

6.6.2、根目录创建layouts目录,/layouts/default.vue

<template>
    <div>
        snow-default-layout
    </div>
    <slot />
</template>
<script setup lang="ts">
</script>
<style scoped>
</style>

6.6.3、app.vue

<template>
  <NuxtLayout>
    <NuxtPage/>
  </NuxtLayout>
  <!-- <div>
    <NuxtWelcome />
  </div> -->
</template>

6.6.4、

6.6.5、某页面不想使用layout:

definePageMeta({
    layout: false
})

6.6.6、某页面使用指定layout

definePageMeta({
    layout: 'layout-snow'
})

 6.6.7、动态设置layout

const router = useRoute()
function enableLayout () {
    router.meta.layout = "layout-snow"
}

6.6.8、全局设置,指定layout

<template>
  <NuxtLayout :name="layoutSnow">
    <NuxtPage/>
  </NuxtLayout>
  <!-- <div>
    <NuxtWelcome />
  </div> -->
</template>
<script setup lang="ts">
const layoutSnow = ref("layout-snow")
</script>

6.7、plugins

/plugins/index.ts

export default defineNuxtPlugin(()=>{
    return {
        provide: {
            hello: (msg: string) => `hello ${msg}`
        }
    }
})

/pages/plugin.vue

<template>
    <div>
        snow-plugin
    </div>
</template>
<script setup lang="ts">
const { $hello } = useNuxtApp()
console.log('9', $hello('world'))
</script>
<style scoped>
</style>

6.8、middleware

/middleware/auth.ts

export default defineNuxtRouteMiddleware((to, from)=>{
    console.log("auth")
})

/middleware/snow.ts

export default defineNuxtRouteMiddleware((to, from)=>{
    console.log("snow")
})

/pages/plugin.vue

<template>
    <div>
        snow-plugin
    </div>
</template>
<script setup lang="ts">
definePageMeta({
    middleware: ["auth", "snow"]
})
</script>
<style scoped>
</style>

如图:

6.9、server

6.9.1、目录

6.9.2、server/api/hello.ts

export default defineEventHandler((event) => {
    return {
        api: "works"
    }
})

6.9.3、server/api/test.get.ts

export default defineEventHandler((event) => {
    return `test get ts`
})

6.9.4、server/api/test.post.ts

export default defineEventHandler((event) => {
    return `test post ts`
})

post请求需要使用postman工具:

6.9.5、 server/api/foo/[...].ts

可以带有参数

export default defineEventHandler(() => `default api handle`)

6.9.6、server/api/submit.post.ts

export default defineEventHandler (async (event) => {
    const body = await readBody(event);
    return { body }
})

6.9.7、server/api/query.get.ts

export default defineEventHandler((event) => {
  const query = getQuery(event)
  return { a: query.param1, b: query.param2 }
})

6.10、使用element-plus

6.10.1、安装

pnpm install element-plus

pnpm add  sass sass-loader -D

pnpm add unplugin-auto-import unplugin-icons unplugin-vue-components -D

6.10.2、package.json

{
    "private":true,
    "scripts":{
        "build":"nuxt build",
        "dev":"nuxt dev",
        "generate":"nuxt generate",
        "preview":"nuxt preview",
        "postinstall":"nuxt prepare"
    },
    "devDependencies":{
        "nuxt":"3.0.0",
        "sass":"1.57.1",
        "sass-loader":"13.2.8",
        "unplugin-auto-import":".12.1",
        "unplugin-icons":".14.15",
        "unplugin-vue-components":".22.12"
    },
    "dependencies":{
        "element-plus":"^2 .2.27"
    }
}

6.10.3、assets/scss/index.scss

@use "element-plus/dist/index.css";

6.10.4、tsconfig.json

{
  // https://nuxt.com/docs/guide/concepts/typescript
  "extends": "./.nuxt/tsconfig.json",
  "compilerOptions": {
    "types": ["element-plus/global"]
  }
}

6.10.5、 nuxt.config.ts

import AutoImport from 'unplugin-auto-import/vite'
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import IconsResolver from "unplugin-icons/resolver";
const lifecycle = process.env.npm_lifecycle_event;
export default defineNuxtConfig({
    vite: {
        plugins: [
            AutoImport({
                resolvers: [
                    ElementPlusResolver(
                    ),
                    IconsResolver()]
            }),
            Components({
                dts: true,
                resolvers: [ElementPlusResolver(
                    {
                        importStyle: false
                    }
                )]
            }),
        ],
    },
    components: true,
    css: ["~/assets/scss/index.scss"],
    transpile: ["element-plus"],
    build: {
        transpile: lifecycle === "build" ? ["element-plus"] : [],
    },
})

6.10.6、plugins/element-plus.ts

import { ID_INJECTION_KEY } from 'element-plus';
export default defineNuxtPlugin(nuxtApp => {
    // Doing something with nuxtApp
    nuxtApp.vueApp.provide(ID_INJECTION_KEY,{
        prefix: Math.floor(Math.random() * 10000),
        current: 0,
    })
  })

6.10.7、pages/index.vue

<el-button> ElButton </el-button>

6.10.8、成功实现效果

相关文章
|
JavaScript 中间件 API
nuxt3:我们开始吧-开发-配置-部署(一)
nuxt3:我们开始吧-开发-配置-部署(一)
993 0
|
存储 应用服务中间件 nginx
nuxt3:我们开始吧-开发-配置-部署(三)
nuxt3:我们开始吧-开发-配置-部署(三)
1157 0
|
1月前
|
移动开发 小程序 数据可视化
基于npm CLI脚手架的uniapp项目创建、运行与打包全攻略(微信小程序、H5、APP全覆盖)
基于npm CLI脚手架的uniapp项目创建、运行与打包全攻略(微信小程序、H5、APP全覆盖)
204 3
|
3月前
|
资源调度 JavaScript 前端开发
搭建和部署nuxt项目
【8月更文挑战第4天】
|
3月前
Nuxt项目的安装和使用
【8月更文挑战第7天】
41 2
|
4月前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的网络互联实验平台附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的网络互联实验平台附带文章源码部署视频讲解等
52 0
|
4月前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的批量运维管理系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的批量运维管理系统附带文章源码部署视频讲解等
38 0
|
域名解析 开发框架 JavaScript
vue2.0项目从零开发到打包部署
vue2.0项目从零开发到打包部署
143 0
|
JavaScript 前端开发 jenkins
jenkins上部署前端vue项目
jenkins上部署前端vue项目
189 0