nuxt3项目搭建相关

nuxt3项目搭建相关

1. 创建项目

powershell 复制代码
npx nuxi@latest init <project-name>

2. 配置环境变量

环境变量文件

package.json配置启动项

js 复制代码
{
  "name": "nuxt-app",
  "private": true,
  "type": "module",
  "scripts": {
    "build": "nuxt build --dotenv .env.production",
    "build:dev": "nuxt build --dotenv .env.development",
    "build:test": "nuxt build --dotenv .env.test",
    "dev": "nuxt dev --dotenv .env.development --exec",
    "test": "nuxt dev --dotenv .env.test --exec",
    "prod": "nuxt dev --dotenv .env.production --exec",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare"
  },
  "dependencies": {
    "nuxt": "^3.14.1592",
    "vue": "latest",
    "vue-router": "latest"
  }
}

nuxt.config.ts配置

js 复制代码
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  runtimeConfig: {
    public: {
      APP_BASE_API: process.env.VITE_APP_BASE_API,
      APP_OPERATION_URL: process.env.VITE_APP_OPERATION_URL,
    }
  },
  compatibilityDate: '2024-11-01',
  devtools: { enabled: true }
})

在页面使用

两种方式

html 复制代码
<template>
  <div>
    {{ env.VITE_APP_BASE_API }}
    {{ APP_BASE_API }}
  </div>
</template>

<script lang="ts" setup>
const env = import.meta.env;

import { useRuntimeConfig } from '#app'
const config = useRuntimeConfig()
const APP_BASE_API = config.public.APP_BASE_API
</script>
相关推荐
柚子8165 小时前
break跳出语句块的神奇技巧
前端·javascript
万法若空5 小时前
C++ <memory> 库全方位详解
开发语言·c++
代码中介商5 小时前
C++ 类型转换深度解析:static_cast、dynamic_cast、const_cast、reinterpret_cast
开发语言·c++
青小莫5 小时前
C++之string(OJ练习)
开发语言·c++·stl
freshman_y5 小时前
一篇介绍C语言中二级指针和二维数组的文章
c语言·开发语言
-Marks-5 小时前
【C++编程】STL简介 --- (是什么 | 版本发展历程 | 六大组件 | 重要性缺陷以及如何学习)
开发语言·c++·学习·stl·stl版本
HealthScience6 小时前
【Bib 2026】基因最新综述(有什么任务、benchmark、代表性模型)
android·开发语言·kotlin
wjs20246 小时前
CSS 网格元素
开发语言
ejinxian6 小时前
Rust GUI框架Azul与Electron、WebView2
前端·javascript·electron