Ember应用程序的多个路由器

时间:2015-05-26 08:49:11

标签: javascript node.js ember.js ember-cli ember-router

Ember应用程序可以有多个router.js吗?

默认情况下,一个router.js将具有

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

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

export default Router.map(function() {
  this.resource('sampleroute');

});

和其他router.js将有

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

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

export default Router.map(function() {
  this.resource('sampleroute2');

});

我只需要让Ember应用程序读取我的第二个路由器(router2.js),默认情况下会读取默认的router.js以设置路由。

2 个答案:

答案 0 :(得分:0)

您可以在函数内的seprate文件中定义ur路由,并导出该函数,如

page1.js

export default function() {
  this.route('page1');
}

page2.js

export default function() {
  this.route('page2');
}

然后将其导入router.js文件。

import page1Routes from 'page1';
import page2Routes from 'page2';

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

Router.map(function() {
  page1Routes.call(this);
  page2Routes.call(this);       
});

export default Router;

您可以重复调用Router.map来添加新路由。所以你可以用同样的方式导入你的其他路线。

答案 1 :(得分:0)

您可以将您的功能分开显示为above的blessenm。 但在这种情况下,您将继续保持对这些文件的依赖。而router.js仍然是几乎所有开发人员都使用的资源。

除了答案: 对于每个模块,您可以生成导出该函数的“modul-route.js”文件。然后通过使用instance-initializers,您可以收集所有这些函数并为所有加载的文件运行 Router.map(...); 函数。