📖 Scenario: You are building a Vue app that needs to show different messages depending on the environment it runs in, like development or production.
🎯 Goal: Create a Vue 3 app that reads an environment variable and displays it on the page.
📋 What You'll Learn
Create a Vue 3 app with App.vue component
Use environment variables with the import.meta.env syntax
Display the environment variable value in the template
Use a config variable to hold the environment variable value
💡 Why This Matters
🌍 Real World
Many Vue apps need to behave differently in development, testing, and production. Environment variables help manage these differences safely.
💼 Career
Understanding environment variables is essential for frontend developers to configure apps without hardcoding sensitive or environment-specific data.
Progress0 / 4 steps
1
Create the Vue app skeleton
Create a Vue 3 single file component called App.vue with a <template> section containing a <div> with the text Environment: . Also add an empty <script setup> section.
Vue
Hint
Start by writing the basic Vue component structure with template and script setup tags.
2
Add a config variable for the environment
Inside the <script setup> section, create a constant called envMessage and set it to import.meta.env.VITE_APP_ENV.
Vue
Hint
Use const envMessage = import.meta.env.VITE_APP_ENV to read the environment variable.
3
Display the environment variable in the template
Modify the <div> inside the <template> to show the text Environment: followed by the value of envMessage using Vue's mustache syntax.
Vue
Hint
Use {{ envMessage }} inside the div to show the variable.
4
Add a fallback for missing environment variable
Update the envMessage constant to use the value of import.meta.env.VITE_APP_ENV or fallback to the string 'unknown' if it is not set, using the logical OR operator.
Vue
Hint
Use const envMessage = import.meta.env.VITE_APP_ENV || 'unknown' to provide a fallback.
Practice
(1/5)
1. In Vue projects, why do environment variables usually start with VITE_?
easy
A. Because it encrypts the variables for security.
B. Because VITE_ is a required prefix for all JavaScript variables.
C. Because it makes variables global across all projects automatically.
D. Because Vue only exposes variables starting with VITE_ to the client code.
Solution
Step 1: Understand Vue environment variable rules
Vue only exposes environment variables to client code if they start with VITE_ to avoid leaking sensitive data.
Step 2: Analyze the options
Only Because Vue only exposes variables starting with VITE_ to the client code. correctly states this behavior. Other options are incorrect or unrelated.
Final Answer:
Because Vue only exposes variables starting with VITE_ to the client code. -> Option D
Quick Check:
Vue env vars need VITE_ prefix = A [OK]
Hint: Remember Vue only exposes vars starting with VITE_ [OK]
Common Mistakes:
Thinking all env vars are exposed regardless of prefix
Confusing prefix with encryption or global scope
Assuming prefix is optional
2. Which of the following is the correct way to access an environment variable named VITE_API_URL inside a Vue component?