Jelly427
V2EX  ›  Nuxt.js

nuxt 部署在 cf Page 上 想用 env 里的环境变量

  •  
  •   Jelly427 · Apr 30, 2024 via Android · 4624 views
    This topic created in 767 days ago, the information mentioned may be changed or developed.
    nuxt 部署在 cf Page 上 想用 env 里的环境变量

    或者这样说 cf 里的环境变量 如何在 nuxt 里获取

    菜鸟搞了 2 天没成功
    4 replies    2024-04-30 14:29:56 +08:00
    cuttlefish
        1
    cuttlefish  
       Apr 30, 2024
    npm install dotenv --save-dev
    import dotenv from 'dotenv';
    const myVariable = process.env.VITE_MY_VARIABLE;
    sparkinglemon
        3
    sparkinglemon  
       Apr 30, 2024
    楼上给的是 worker 的,现在 pages 部署略有不同,

    1. Cloudflare Pages 官方给的 Nuxt 接入: https://developers.cloudflare.com/pages/framework-guides/deploy-a-nuxt-site
    2. 关于环境变量在 Pages 的 functions 里的使用: https://developers.cloudflare.com/pages/functions/bindings/
    3. 如果只是希望打包时(比如 SSG )读取环境变量,在对应的 pages 面板,"设置" -> "环境变量" 里可以设置,然后就按 nuxt 正常读取方式从 process.env 里读就行
    Jelly427
        4
    Jelly427  
    OP
       Apr 30, 2024
    @sparkinglemon 感谢 我去试试
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2857 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 01:54 · PVG 09:54 · LAX 18:54 · JFK 21:54
    ♥ Do have faith in what you're doing.