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>
相关推荐
灯澜忆梦1 小时前
GO_并发编程---定时器
开发语言·后端·golang
-银雾鸢尾-1 小时前
C#中的StringBuilder相关方法
开发语言·c#
To_OC1 小时前
LC 42 接雨水:暴力超时卡半天?前后缀数组一用就通了
javascript·算法·leetcode
小林ixn2 小时前
从 ??= 到 onKeyDown:一个 React 组件的“自我修养”
前端·javascript·react.js
-银雾鸢尾-2 小时前
C#中结构体与类的区别;抽象类与接口的区别
开发语言·c#
এ慕ོ冬℘゜2 小时前
前端实战:jQuery 多条件联合搜索(标题模糊查询 + 日历时间段筛选)
前端·javascript·jquery
大模型码小白2 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
随心点儿3 小时前
js postMessage 实现跨域发送消息-应用示例
javascript·ecmascript·postmessage
kyriewen4 小时前
我让AI改一个bug——它偷偷动了5个我没让它碰的地方
前端·javascript·ai编程
段一凡-华北理工大学4 小时前
向量数据库实战:选型、调优与落地~系列文章12:文本分块策略实战:chunk_size 怎么选?重叠多少?
开发语言·数据库·后端·oracle·rust·工业智能体·高炉智能化