【问题标题】:Backbone : Remove hash from front in URLBackbone :从 URL 的前面删除哈希
【发布时间】:2016-12-13 17:26:33
【问题描述】:

我用 Backbone 和 Marionette 创建了一个简单的应用程序。它工作正常,没有错误或类似的东西。但是当我看到网址时,网址前面有一个哈希,这是我的链接

programming/index.html#chart 
programming/index.html#tutorial/3 
programming/index.html#tutorial/6

我想让它更干净、更易读,我想删除标签并替换为/,这样 URL 看起来像这样

programming/index.html/chart
programming/index.html/tutorial/3
programming/index.html/tutorial/6

也可以访问这个

programming/chart
programming/tutorial/3
programming/tutorial/6

我尝试了很多这个问题的答案

Routing in Backbone.js / Marionette.js - no hashtags, route list and sub-routers

BackboneJS - Router issues - how to get clean URL's

我试着让 pushState 为 true,像这样

programming.on('start',function(){
    if(Backbone.history){
        Backbone.history.start({
            pushState : true
        });  

        if(this.getCurentRoute() === ""){
            programming.trigger("program:list")
        }
    }
})

我尝试制作一个 .htaccess 文件

<IfModule mod_rewrite.c>
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)$ /#$1 [R,L,NE]
</IfModule>

但是所有这些都不适合我:(请大家帮帮我。

【问题讨论】:

  • 我不认为你可以这样做,因为如果你删除散列,那么它将用于服务器端路由而不是客户端路由,并且你需要非 HTML5 浏览器的散列

标签: javascript backbone.js marionette


【解决方案1】:

这对我有用。

$('body').delegate('a[href]:not([href^=\#])', 'click', function (e) {
   e.preventDefault();
   Backbone.history.navigate($(this).attr('href'), {trigger: true});
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-29
    • 2012-04-19
    • 2013-05-15
    • 2013-03-02
    • 1970-01-01
    • 2017-05-31
    • 2018-01-23
    相关资源
    最近更新 更多