Routing 萨米不工作
我无法使sammy.js路由按预期工作 我有以下JavaScriptRouting 萨米不工作,routing,sammy.js,Routing,Sammy.js,我无法使sammy.js路由按预期工作 我有以下JavaScript (function ($) { var app = $.sammy('#main', function () { this.get('#/', function (context) { context.log("start page"); }); this.get("#/another/:id/", function (context) {
(function ($) {
var app = $.sammy('#main', function () {
this.get('#/', function (context) {
context.log("start page");
});
this.get("#/another/:id/", function (context) {
context.log("another page with id = " + context.params["id"]);
});
});
$(function (context) {
app.run('#/');
});
})(jQuery);
我的理解是,每当URL发生更改时,只要新路由对应于声明的路由之一,我就应该在控制台日志中看到相关消息
但我没有
当第一次加载页面时,我可以看到“开始页面”消息。到目前为止还不错
如果我当时
- 单击指向[我的url]/#/另一个/1的链接/
- 在地址栏中手动键入[my url]/#/Other/1/,然后点击
我是否误解了,或者Sammy是否应该注意到超链接或键盘改变了URL哈希,然后采取相应的行动?还有什么需要添加到脚本中的吗?每个sammy应用程序都需要附加到DOM元素 因此,您可能缺少页面中的
#main
元素
因此,您需要在DOM中的某个位置添加一个#main
元素:
<div id="main">
</div>
在这种情况下,它将自身附加到主体
元素
var app = $.sammy(function () {
this.get('#/', function (context) {
context.log("start page");
});
this.get("#/another/:id/", function (context) {
context.log("another page with id = " + context.params["id"]);
});
});