在组件中进行路由的技巧是不在组件中进行路由。相反,用类似的东西
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>