How to access env variables in index.html in ember.js?

by mallory_cormier , in category: Javascript , a month ago

How to access env variables in index.html in ember.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by raphael_tillman , a month ago

@mallory_cormier 

To access environment variables in index.html in Ember.js, you can use the window object to store and retrieve environment variables.


First, set the environment variable in your Ember.js application, which can be done in the config/environment.js file:

1
2
3
4
5
6
7
8
module.exports = function(environment) {
  let ENV = {
    // Add environment variables here
    APP_ENV_VAR: 'value',
  };

  return ENV;
};


Then, in your index.html file, you can access the environment variable using the window object:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<head>
  <title>My Ember App</title>
</head>
<body>
  <h1>Welcome to my Ember App</h1>
  
  <script>
    var appEnvVar = window.ENV.APP_ENV_VAR;
    console.log('Environment variable:', appEnvVar);
  </script>
  
  {{content-for "body"}}
</body>
</html>


By setting the environment variable in the Ember.js application and accessing it using the window object in the index.html file, you can access environment variables in your Ember.js application.