简体   繁体   English

Ember 全局应用变量

[英]Ember global App variable

I am developing my Ember as an addon & inside my tests\\dummy\\app.js, I have我正在开发我的 Ember 作为插件 & 在我的测试\\dummy\\app.js 中,我有

App = Ember.Application.extend({
  modulePrefix: config.modulePrefix,
});

My question is related to accessing global variable acrosss the Ember app.我的问题与跨 Ember 应用程序访问全局变量有关。

Do I have to do我必须做吗

window.App = App;

in the above place only (ie where I'll have Ember.Application.extend())仅在上面的地方(即我将拥有 Ember.Application.extend() 的地方)

OR can I do in some other place as well ?或者我也可以在其他地方做吗?

You can define a class in a js file such as:您可以在 js 文件中定义一个类,例如:

import Ember from 'ember';

const MyClazz = Ember.Object.extend({
    // your properties and methods
});

const myObj = MyClazz.create({});
export default myObj;

This object will behave as a global object and you can access this object from anywhere you want like that:这个对象将作为一个全局对象,你可以从任何你想要的地方访问这个对象:

import MyObj from '/...path';
...

MyObj.get('...');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM