To specify the base URL when using useFetch
in Nuxt with TypeScript, you can create a separate fetch
utility function that sets the base URL and returns a new useFetch
hook with pre-configured options.
Here is an example implementation for this approach:
index.ts652 chars23 lines
In this example, we have defined a separate fetchAPI
utility function that sets the base URL to https://api.example.com
. We then use this function to create a pre-configured useFetch
hook with the useAPIFetch
function.
To use this hook in your component, import useAPIFetch
from your custom fetch.ts
file:
index.ts304 chars17 lines
In this example, we use useAPIFetch
instead of the standard useFetch
hook, and the base URL is automatically set to https://api.example.com
whenever a request is made.
gistlibby LogSnag