Nuxt3ã§APIã®URLãªã©ã®è¨å®å¤ã«ã¤ãã¦ã¿ã¦ãããã
å®ã¯runtimeConfig
以å¤ã«ããããããã£ãã®ã§èª¿ã¹ã¦ã¿ãã¨ãã®åå¿é²(*´Ïï½*)
nuxt.config.ts
ã®runtimeConfig
ãã¤ãã®ãã¤ãNuxt3ããã¯composablesãæä¾ããã
åæ¨è«ãå¹ãããã«ãªã£ã¦ããã
// nuxt.config.ts export default defineNuxtConfig({ runtimeConfig: { // Private keys are only available on the server apiSecret: '123', // Public keys that are exposed to the client public: { apiBase: process.env.NUXT_PUBLIC_API_BASE || '/api' } } })
export default async () => { const config = useRuntimeConfig() // Access public variables const result = await $fetch(`/test`, { baseURL: config.public.apiBase, headers: { // Access a private variable (only available on the server) Authorization: `Bearer ${config.apiSecret}` } }) return result }
ç°å¢å¤æ°(NUXT_*)
nuxt.config.ts
ã®runtimeConfig
ã®.env
ã«ãã代æ¿ãã
NUXT_
ã§ã¯ãããã¨runtimeConfig
ã«èªåã§è¨å®ãã¦ãããã
ãã ãåå®ç¾©ãç¨æãã¦ãããªãã®ã§ãä¸é·ä¸çãã
# .env # runtimeConfig.apiSecret NUXT_API_SECRET = "123" # runtimeConfig.public.apiBase NUXT_PUBLIC_API_BASE = "https://api.localhost:5555"
app.config.ts
ãã¹ã¦publicã®è¨å®å¤ã®config.tsã
nuxt.config.tsãå¤æ´ããã¨ãnuxtãåèµ·åãå¿
è¦ã ãã©ã
å¥ãã¡ã¤ã«ãªã®ã§ãnuxtãåèµ·åããã«å¤æ´ã§ããã
publicãªãã®ã§ããã°ããã¡ãã®ã»ããéçºæã¯ä¾¿å©(*´Ïï½*)
export default defineAppConfig({ theme: { primaryColor: '#ababab' } })
const appConfig = useAppConfig(); console.log(appConfig.theme);
以ä¸!! ããããç¹å¾´ãããã®ã§ã使ãåãã¦ãããã(*´Ïï½*)