ember-cli App.get/App.set throw undefined

run*_*red 3 ember.js ecmascript-6 ember-cli

我正在将基于全局变量的实时Ember应用程序转换为使用ember-cli的基于es6的应用程序.在我的应用程序中,我需要经常了解当前的路线.在全局版本中,我正在这样做.

全球模式

var MyApp = Ember.Application.create({
    currentRoute : ''
});

MyApp.Route = Ember.Route.extend({
    actions : {
        didTransition : function () {
            MyApp.set('currentRoute', this);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

然后,我可以MyApp.get('currentRoute')在我的会话或离线控制器中确定在发生某些事件时如何/在何处转换.

使用ember-cli时,我导入应用程序以便能够从必要的控制器中引用它.

import MyApp from "../app";
Run Code Online (Sandbox Code Playgroud)

但事实证明MyApp.currentRoute,MyApp.getMyApp.set都是不确定的.

我的一部分认为这是ember-cli中的一个错误,即应用程序实例不再绑定getter和setter.我的一部分意识到将应用程序实例存储在一起并不是一个很好的做法.

我可以解决这个问题,通过转换的所有实例MyApp.get,并MyApp.setEmber.get(MyApp, ...)Ember.set(MyApp, ...)分别,但我想我会先问这里,因为这似乎要么是与Ember,CLI或其他什么东西的问题,其中有达到什么更好的推荐方式我需要.

ato*_*irk 6

如果你看一下app.js(要导入的),这是不是你的应用程序实例,它导出的子类Ember.Application.这就是为什么getet al不可用的原因.

var App = Ember.Application.extend({   <----- NOT create
    modulePrefix: config.modulePrefix,
    podModulePrefix: config.podModulePrefix,
    Resolver: Resolver
});

export default App;
Run Code Online (Sandbox Code Playgroud)

要从您的路径中获取实际应用程序实例,请使用:

Ember.getOwner(this).lookup('application:main')
Run Code Online (Sandbox Code Playgroud)