コンポーネントでルーティングを行うコツは、コンポーネントでルーティングを行わないことです。代わりに、次のようなもので
can.route(":page/:id", {page: "content", id: "index"});
can.route
状態オブジェクトとして渡します。次のようなメイン ビューを使用します。
<script id="main" type="text/mustache">
<app-page page="state.page" page-id="state.id"></app-page>
</script>
コンポーネントのようにレンダリングされ、can.view('main', { state: can.route })
それらの属性をチェックするだけです:
can.Component.extend({
tag: 'app-page',
template: 'page',
scope: {
isBlog: function() {
return this.attr('page') === 'blog';
},
isStatic: function() {
return this.attr('page') === 'content';
}
}
});
子コンポーネントを初期化するビューの場合:
<script id="page" type="text/mustache">
{{#if isBlog}}
<app-blog blog-id="pageId"></app-blog>
{{/if}}
{{#if isStatic}}
<app-static page-id="pageId"></app-static>
{{/if}}
</script>