【问题标题】:Canjs routing componentsCanjs 路由组件
【发布时间】:2014-10-05 00:12:59
【问题描述】:

如何使用 canjs 完成组件路由?
我有以下示例

can.Component.extend({
    tag: "router",
    events: {
      "{can.route} id bla": function(route, event, id, bla) {
        console.log("route", id, bla);
      }
    }
  });

如何匹配具体路线?例如 page/2/foo.
路由定义为

can.route(":page/:id/:bla", {page: "", id: "", bla: ""});

【问题讨论】:

    标签: javascript url-routing canjs canjs-routing


    【解决方案1】:

    在组件中进行路由的技巧是不在组件中进行路由。相反,像

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多