我想用 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/