@mallory_cormier
To import a global variable in Ember.js, you can use the Ember.inject.service() function or the Ember.getOwner() method.
Here is an example using Ember.inject.service():
1 2 3 4 5 6 |
// app/services/global.js
import Service from '@ember/service';
export default Service.extend({
myVariable: 'Hello'
});
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// app/components/my-component.js
import Component from '@ember/component';
import { inject as service } from '@ember/service';
export default Component.extend({
global: service(),
init() {
this._super(...arguments);
console.log(this.get('global.myVariable')); // Outputs 'Hello'
}
});
|
Alternatively, you can also use Ember.getOwner() to access the global variable directly:
1 2 3 4 |
import { getOwner } from '@ember/application';
let global = getOwner(this).lookup('service:global');
console.log(global.myVariable); // Outputs 'Hello'
|
These methods allow you to import and access global variables in Ember.js components and controllers.