Ember App.router。 “App未定义”

时间:2015-09-23 16:01:16

标签: javascript ember.js

我第一次尝试路由我的应用程序,但我不太了解逻辑。当我把route.js文件放到以下文件时,我得到了我的模板:

import Ember from 'ember';
import config from './config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

Router.map(function() {
  this.route('main', { path: '/' }, function() {

    this.route('helloworld', { path: '/'});
  });
});

export default Router;

大多数示例似乎都使用App.router.map函数进行路由。首先我不太明白有什么区别?路线是存储在应用程序内还是什么。当我尝试将其添加到route.js时:

import Ember from 'ember';
import config from './config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

App.Router = Ember.Router.extend({
  enableLogging: true,
  location: 'hash'
});

App.Router.map(function() {
  this.route('main');
});

export default Router;

我得到:“未捕获的ReferenceError:App未定义”。那么这里的正确方法是什么?我错过了什么?

1 个答案:

答案 0 :(得分:2)

通常,有两种创建Ember应用程序的方法:

  1. 使用推荐的Ember CLI
  2. 使用全局变量 - App.Router等
  3. 在Ember CLI中,您永远不应该访问全局app变量。因此,您的router.js应该在您的第一个代码示例中看起来像。

    App未在router.js内定义,因为它不适合使用Ember CLI。

    在Ember CLI中,您使用分散在多个目录中的模块和文件,而不是使用全局变量的{1}大文件。

    您应该了解更多有关Ember CLI和Ember的基础知识。 Ember Guides是最佳起点。