简体   繁体   English

Crossroadsjs路由:如何使用?

[英]Crossroadsjs routing: how to use it?

I am facing the same problem as this one as I am trying to figure out how to use crossroads for a few hours now and nothing seems to work. 我正面临与问题相同的问题,因为我现在试图弄清楚如何使用十字路口几个小时,而且似乎没有任何效果。 its webiste is just another poor documented site... I think I am probably daft! 它的webiste只是另一个记录较差的网站...我想我可能很傻! I wonder if anyone has made it? 我想知道是否有人做到了?

html head, html头,

<title>Crossroads</title>
  <script src="js/libs/signals.js"></script>
  <script src="js/libs/crossroads.min.js"></script>
  <script src="js/app.js"></script>
</head>

app.js, just as simple as this, app.js就这么简单,

crossroads.addRoute('/news/{id}', function(id){  
  alert(id);  
}); 

so I try it out on my localhost browser, 所以我在本地浏览器上尝试一下

http://localhost/crossroadjs/#/news/123

nothing happens. 什么都没发生。 I thought it would be 123 ?? 我以为会是123 ??

Crossroads doesn't handle history/state change events from the browser. Crossroads不会处理浏览器中的历史记录/状态更改事件。 From their site : 他们的网站

A routes system shouldn't do anything else besides routing. 路由系统除路由外不应执行任何其他操作。

Instead, the site recommends Hasher for this purpose and gives a rather complete looking example: 相反,该网站为此推荐了Hasher并给出了一个相当完整的示例:

//setup crossroads
crossroads.addRoute('foo');
crossroads.addRoute('lorem/ipsum');
crossroads.routed.add(console.log, console); //log all routes

//setup hasher
function parseHash(newHash, oldHash){
  crossroads.parse(newHash);
}
hasher.initialized.add(parseHash); //parse initial hash
hasher.changed.add(parseHash); //parse hash changes
hasher.init(); //start listening for history change

//update URL fragment generating new history record
hasher.setHash('lorem/ipsum');

Alternatively you could use a different history plugin, or write something yourself. 或者,您可以使用其他历史记录插件,也可以自己编写。 But crossroads leaves that part up to you. 但是,十字路口让您分心。

Crossroads.js gives crossroads.addRoute(pattern, [handler], [priority]); Crossroads.js给出crossroads.addRoute(pattern, [handler], [priority]); API to add route patterns. 用于添加路由模式的API。 However, when the first time you load the page Crossroads does not automatically initiate the parser to check against the url of the page. 但是,当您第一次加载页面时,Crossroads不会自动启动解析器来检查页面的url。 You need to add crossroads.parse(document.location.pathname); 您需要添加crossroads.parse(document.location.pathname); on you document load to trigger the route. 在文档加载时触发路线。 Check out Crossroads.js Tutorial . 查看Crossroads.js教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM