Challenge - 5 Problems
Environment Variables Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does Vue handle environment variables in templates?
Given a Vue 3 component that uses
import.meta.env.VITE_API_URL inside its template, what will be rendered if VITE_API_URL is set to https://api.example.com?Vue
<template>
<div>{{ import.meta.env.VITE_API_URL }}</div>
</template>
<script setup>
// no script needed
</script>Attempts:
2 left
💡 Hint
Remember that
import.meta.env is replaced at build time and can be used in templates.✗ Incorrect
Vue replaces import.meta.env.VITE_API_URL with the actual environment variable value at build time, so the string is rendered directly.
📝 Syntax
intermediate1:30remaining
Correct syntax to access environment variables in Vue script setup
Which of the following is the correct way to access the environment variable
VITE_APP_TITLE inside a Vue 3 <script setup> block?Vue
<script setup>
// Access environment variable here
</script>Attempts:
2 left
💡 Hint
Vue 3 uses Vite which exposes env variables via import.meta.env.
✗ Incorrect
In Vue 3 with Vite, environment variables are accessed using import.meta.env. The other options are invalid or undefined.
🔧 Debug
advanced2:00remaining
Why does the environment variable not update after changing .env file?
You changed the value of
VITE_API_KEY in your .env file but your Vue app still shows the old value. What is the most likely reason?Attempts:
2 left
💡 Hint
Think about how build tools load environment variables.
✗ Incorrect
Vite loads environment variables at server start. Changes in .env require restarting the dev server to take effect.
🧠 Conceptual
advanced1:30remaining
Security considerations for Vue environment variables
Which statement about environment variables in Vue apps built with Vite is true?
Attempts:
2 left
💡 Hint
Consider what happens to variables during build time and client bundling.
✗ Incorrect
Vite exposes all variables starting with VITE_ to client code. They are bundled and visible in browser devtools.
❓ state_output
expert2:30remaining
Effect of environment variable change on reactive state
Consider this Vue 3 component snippet:
If you change
<script setup>
import { ref } from 'vue'
const apiUrl = ref(import.meta.env.VITE_API_URL)
</script>
<template>
<div>API URL: {{ apiUrl }}</div>
</template>If you change
VITE_API_URL in the .env file and restart the dev server, what will happen to the displayed apiUrl value when the app reloads?Attempts:
2 left
💡 Hint
Think about when environment variables are read and how refs work on reload.
✗ Incorrect
On reload, import.meta.env.VITE_API_URL is read fresh and assigned to the ref, so the displayed value updates accordingly.