简体   繁体   English

如何在Ember CLI应用程序中访问Ember全局“App”变量?

[英]How do I access the Ember global 'App' variable in an Ember CLI application?

I am creating an Ember application using the Ember CLI. 我正在使用Ember CLI创建Ember应用程序。 I have a view which invokes a component that I created. 我有一个视图调用我创建的组件。 I am trying to access the global App variable to create my component and insert it into my layout. 我试图访问全局App变量来创建我的组件并将其插入我的布局。

The error: Uncaught ReferenceError: App is not defined 错误:未捕获的ReferenceError:未定义App

How do I fix this? 我该如何解决?

app.js app.js

import Ember from 'ember';
import Resolver from 'ember/resolver';
import loadInitializers from 'ember/load-initializers';

Ember.MODEL_FACTORY_INJECTIONS = true;

var App = Ember.Application.extend({
  modulePrefix: 'client-web', // TODO: loaded via config
  Resolver: Resolver
});

loadInitializers(App, 'client-web');

export default App;

item-table.js (This is a view) item-table.js (这是一个视图)

import Ember from 'ember';

export default Ember.View.extend({
    templateName: 'item-table',

    didInsertElement: function() {
        // All my other code here

        App.FreestyleChartComponent.create().appendTo($('#wp-chart td')); // This throws an error.
    }
});

app/components/freestyle-chart.js 应用/组件/自由式-chart.js之

import Ember from 'ember';

export default Ember.Component.extend({
    templateName: 'components/freestyle-chart',

    didInsertElement: function() {
        console.log('Inserted the component.');
    }
});

I can think of two ways. 我可以想到两种方式。 The first is to put the App in the global scope manually. 第一种是手动将App放在全局范围内。

var App = window.App = Ember.Application.extend();

The second is to import the App into your view: 第二种是将App导入您的视图:

import App from './path/to/app/file';

The latter is only possible if Ember CLI supports circular references. 只有Ember CLI支持循环引用才能实现后者。 The official ES6 spec supports them but many transpilers don't (mine doesn't). 官方ES6规范支持它们,但许多转发器不支持(我的不支持)。

However, I don't think this is your root concern. 但是,我不认为这是你的根本关注。 For the most part, you shouldn't be accessing global variables in Ember CLI. 在大多数情况下,您不应该在Ember CLI中访问全局变量。 Instead of placing the FreestyleChartComponent in the App namespace, why not just put it in a module and import it like any other module? 而不是将FreestyleChartComponent放在App命名空间中,为什么不将它放在一个模块中并像任何其他模块一样导入它? Global variables are unavoidable (I experienced that today), but you should try to minimize them. 全局变量是不可避免的(我今天经历过),但你应该尽量减少它们。

I do agree that you should not be accessing your app via the global namespace, however ember-cli actually does actually make you app a global with the name of your app being the name of the variable. 我同意你不应该通过全局命名空间访问你的应用程序,但是实际上ember-cli确实让你的应用程序成为全局应用程序的名称是变量的名称。

If you open /app/index.html in your ember-cli project you should see a script tag towards the bottom that looks like... 如果您在ember-cli项目中打开/app/index.html ,您应该看到一个底部的script标记,看起来像......

<script>
  window.YourAppName = require('your-app-name/app')['default'].create(YourAppNameENV.APP);
</script>

in the above example YourAppName would be your app available as a global 在上面的示例中, YourAppName将是您的应用程序,可用作全局

导入所需的组件:

import FreestyleChartComponent from 'app_name/app/components/freestyle-chart';

There is no straight forward way to do this since Ember CLI wants you to use the public API and its given components rather than accessing the App instance directly. 没有直接的方法可以做到这一点,因为Ember CLI希望您使用公共API及其给定的组件,而不是直接访问App实例。

Most solutions consist of making the App instance global, whereby this one does not. 大多数解决方案都包括使App实例全局化,而不是这样。 I did not test this, but I think this should work: 我没有测试这个,但我认为这应该有效:

appname/utils/application.js 应用程序名称/ utils的/的application.js

export default {
   instance: null
};

appname/instance-initializers/application.js 应用程序名称/实例的初始化/ application.js中

import application from 'appname/utils/application';

export default {
  name: 'app-initializer',
  initialize: function (application) {
     application.instance = application;
  }
};

Now 现在

import application from 'appname/utils/application';

and use application.instance in any file where you need the application instance. 并在需要应用程序实例的任何文件中使用application.instance

Justed tested with Ember 3.7. 用Ember 3.7测试了Justed。

import App from 'app-name/app';

app-name has to be replace with the name of your app (I guess the one in package.json). app-name必须替换为您的应用程序的名称(我猜在package.json中的名称)。

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

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