问题
Is it possible to have multiple router.js for an Ember app?
By default one router.js will have
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');
});
and other router.js will have
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');
});
All I need is to make Ember application to read my second router(router2.js) which by default reads the default router.js to set up routes.
回答1:
You can define ur routes in seprate files inside a function and export that function like
page1.js
export default function() {
this.route('page1');
}
page2.js
export default function() {
this.route('page2');
}
Then import that into your router.js file.
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;
You can call Router.map repeatedly to add new routes. So you can import ur other routes the same way.
回答2:
You can seperate your functions as blessenm shown above. But in that case you are continuing to keep a dependency to those files. And router.js is continuing to be a resource used by nearly all developers.
In addition to that answer: For each modul, you can generate a "modul-route.js" file exporting that function. Then by using instance-initializers, you can collect all this functions and run Router.map(...); function for all loaded files.
来源:https://stackoverflow.com/questions/30453814/multiple-routers-for-an-ember-app