Accessing environment variables in composables in Nuxt3: a guide
P粉739942405
P粉739942405 2023-11-23 13:54:30
0
1
883

In my nuxt 3 application, in order to get data, I want to set a base URL for all API calls. When I get this baseURL from environment variable. How to set baseURL?

I twist with composable useFetch but then I can't get the baseURL because useRuntimeConfig() is not accessible there.

// My composables function
const baseURL = "how to get baseURL from process.env";

export const myFetch = async (url: string) => {
  const options = {
    baseURL: baseURL,
  };

  return await useFetch(url, options);
}


P粉739942405
P粉739942405

reply all(1)
P粉763748806

You can access the runtime configuration by doing this

export default () => {
  const config = useRuntimeConfig()

  console.log(config)
}

As shown below: https://github.com/nuxt/framework /discussions/3215#discussioncomment-3088206

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template