javascript - 如何使用 {can.route} 监听空白路线并使用react?

标签 javascript url-routing canjs canjs-routing

我想用 CanJS 设置路由,以便根据我点击的 url,设置一些相应的控件。我的问题是试图找到一种表达默认路由以在控件中监听的方法:“如果没有匹配,则执行此操作”。这样做有什么技巧吗?

到目前为止,这是我的代码。似乎适用于 /#!/plant/1/day/3/#!/plant/1 等网址,但不适用于 /#!/

can.route('plant/:plant_id/day/:day', {});
can.route('plant/:plant_id', {});
can.route('', {});

var Router = can.Control({}, {
  init : function () {},

  "{can.route}" : function (route, event, newVal, oldVal) {
    console.log('Init default control');
  },

  "{can.route} plant_id" : function (route, event, newVal, oldVal) {
    console.log('Init plant control');
  },

  "{can.route} day" : function (route, event, newVal, oldVal) {
    console.log('Init day control');
  }
});

附言我实际上设法通过这样做使用 Can.Control.route 插件来做到这一点:

"route" : function (route, event, newVal, oldVal) {
    console.log('route Default route', arguments);
}

但是路由插件似乎既可以设置路由又可以对它们使用react,我想知道如何在没有特定插件的情况下做到这一点。

最佳答案

您的解决方案有效,但您也可以使用 can.Control.route允许您直接在 Controller 操作中定义和监听路由的插件:

var Router = can.Control({
    init : function(el, options) {
    },

    "/:plantId route" : function(data) {
        // the route says /id
        // data.id is the id or default value
    },

    route : function(data){
        // the route is empty
    }
});
new Router(window);

关于javascript - 如何使用 {can.route} 监听空白路线并使用react?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19530125/

相关文章:

javascript - jQuery 进度条 PHP 上传未正确响应

javascript - JQuery 提交函数不启动

asp.net-mvc - MVC5中使用WEB API和Area的URL路由

javascript - 如何将 CanJS 与 VelocityJS 一起使用?

javascript - 使用 Chrome 扩展程序检测警报

javascript - 高亮前的 stop() 导致颜色不重置

asp.net - 在 IIS 6 上使用路由部署 ASP.NET 4 Web 表单应用程序

Angular 4 Routing 不适用于实时网络应用程序

javascript - CanJS 对象列表自动子类化和重分类

javascript - 在ejs中添加incrememter到id