0
0
Vueframework~10 mins

Environment variables management in Vue - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to access an environment variable in a Vue component.

Vue
const apiUrl = process.env.[1];
Drag options to blanks, or click blank then click option'
AAPI_URL
BVUE_API_URL
CVUE_APP_API_URL
DAPP_API_URL
Attempts:
3 left
💡 Hint
Common Mistakes
Using environment variables without the VUE_APP_ prefix.
Trying to access variables directly without process.env.
2fill in blank
medium

Complete the code to define a new environment variable in the .env file.

Vue
[1]=https://api.example.com
Drag options to blanks, or click blank then click option'
AAPP_API_URL
BAPI_URL
CVUE_API_URL
DVUE_APP_API_URL
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the VUE_APP_ prefix in the .env file.
Using lowercase letters in the variable name.
3fill in blank
hard

Fix the error in accessing the environment variable inside a Vue component.

Vue
console.log(import.meta.env.[1]);
Drag options to blanks, or click blank then click option'
AVUE_APP_API_URL
BVITE_API_URL
CAPI_URL
DAPP_API_URL
Attempts:
3 left
💡 Hint
Common Mistakes
Using process.env instead of import.meta.env in Vite projects.
Using the Vue CLI prefix VUE_APP_ in Vite projects.
4fill in blank
hard

Fill both blanks to create a computed property that returns the API URL from environment variables.

Vue
import { computed } from 'vue';

const apiUrl = computed(() => [1].env.[2]);
Drag options to blanks, or click blank then click option'
Aimport.meta
Bprocess
CVITE_API_URL
DVUE_APP_API_URL
Attempts:
3 left
💡 Hint
Common Mistakes
Using process.env in Vite projects.
Using the Vue CLI prefix instead of VITE_ prefix.
5fill in blank
hard

Fill all three blanks to create a method that logs the environment variable and handles missing values.

Vue
methods: {
  logApiUrl() {
    const url = [1].env.[2] || [3];
    console.log(url);
  }
}
Drag options to blanks, or click blank then click option'
Aimport.meta
BVITE_API_URL
C'No API URL set'
Dprocess
Attempts:
3 left
💡 Hint
Common Mistakes
Using process.env in Vite projects.
Not providing a fallback value for missing variables.