0
0
Vueframework~20 mins

Environment variables management in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Environment Variables Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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>
AThe string 'https://api.example.com' will be displayed inside the div.
BAn error occurs because <code>import.meta.env</code> cannot be accessed in templates.
CThe literal text 'import.meta.env.VITE_API_URL' will be shown.
DNothing will be displayed because environment variables are not reactive.
Attempts:
2 left
💡 Hint
Remember that import.meta.env is replaced at build time and can be used in templates.
📝 Syntax
intermediate
1: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>
Aconst title = env.VITE_APP_TITLE;
Bconst title = process.env.VITE_APP_TITLE;
Cconst title = import.meta.env.VITE_APP_TITLE;
Dconst title = window.env.VITE_APP_TITLE;
Attempts:
2 left
💡 Hint
Vue 3 uses Vite which exposes env variables via import.meta.env.
🔧 Debug
advanced
2: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?
AYou must import the .env file manually in your component.
BThe variable name must start with VUE_ instead of VITE_.
CEnvironment variables cannot be changed once the app is running.
DYou need to restart the development server to reload environment variables.
Attempts:
2 left
💡 Hint
Think about how build tools load environment variables.
🧠 Conceptual
advanced
1:30remaining
Security considerations for Vue environment variables
Which statement about environment variables in Vue apps built with Vite is true?
AEnvironment variables are only accessible on the server and never bundled in the client code.
BAll environment variables prefixed with VITE_ are exposed to client-side code and visible in the browser.
CVariables without VITE_ prefix are also exposed to the client.
DEnvironment variables are encrypted automatically and safe from client access.
Attempts:
2 left
💡 Hint
Consider what happens to variables during build time and client bundling.
state_output
expert
2:30remaining
Effect of environment variable change on reactive state
Consider this Vue 3 component snippet:
<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?
AThe displayed API URL updates to the new environment variable value after reload.
BThe displayed API URL remains the old value because <code>apiUrl</code> is a ref and does not update automatically.
CThe app crashes because environment variables cannot be assigned to refs.
DThe displayed API URL is empty because environment variables are not reactive.
Attempts:
2 left
💡 Hint
Think about when environment variables are read and how refs work on reload.