Emberjs-router-nested-route
提供:Dev Guides
EmberJS-ルーターのネストされたルート
現在のルートにコールバックを渡すことにより、別のテンプレート内にテンプレートを定義することにより、ネストされたルートを定義できます。
構文
Router.map(function() {
this.route('link-page', { path: 'pathTolinkpag' }, function() {
this.route('link-page');
});
});
ネストされたルートを作成するには、以下のコマンドを実行します-
ember generate route route_name/another_route_name
例
次の例は、あるテンプレートを別のテンプレート内に表示するためのネストされたルートを定義する方法を示しています。 app/templates/nestedroute_の下に作成された.hbs_ファイルを開きます。 ここでは、以下のコードで_fruits.hbs_としてファイルを作成しました-
<h2>Fruits Page</h2>
<ul>
<li>Orange</li>
<li>Apple</li>
<li>Banana</li>
</ul>
_router.js_ファイルを開いてURLマッピングを定義します-
import Ember from 'ember';
//Access to Ember.js library as variable Ember
import config from './config/environment';
//It provides access to app's configuration data as variable config
//The const declares read only variable
const Router = Ember.Router.extend ({
location: config.locationType,
rootURL: config.rootURL
});
//Defines URL mappings that takes parameter as an object to create the routes
Router.map(function() {
this.route('nestedroute', function() {
this.route('fruits');
});
});
//It specifies Router variable available to other parts of the app
export default Router;
_application.hbs_ファイルを作成し、次のコードを追加します-
{{#link-to 'nestedroute.fruits'}}fruits{{/link-to}}
{{outlet}}
出力
エンバーサーバーを実行すると、次の出力が表示されます-
出力のリンクをクリックすると、URLルートが_nestedroute/fruits_として表示され、_fruits.hbs_からの結果が表示されます-