Last active
July 19, 2025 18:56
-
-
Save Kcko/bf4c68a10b7a56e654192cd1d5cc0c26 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// plugins/api.ts | |
export default defineNuxtPlugin((nuxtApp) => { | |
const api = $fetch.create({ | |
baseURL: 'https://api.example.com', | |
onRequest({ options }) { | |
options.headers.set('Authorization', 'Bearer your-token'); | |
}, | |
}); | |
return { | |
provide: { | |
api, | |
}, | |
}; | |
}); | |
Using it in a co |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script setup lang="ts"> | |
const { $api } = useNuxtApp(); | |
const { data } = await $api('/endpoint'); | |
</script> | |
<template> | |
<div>{{ data }}</div> | |
</template> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment