简体   繁体   English

网址中的主干路由器根重复项

[英]Backbone Router root duplicates in the url

I've just finished developing my first Backbone app and I detected a small quirk with the Router which I don't seem to be able to fix. 我刚完成我的第一个Backbone应用程序的开发,并且发现路由器有一个小问题,但似乎无法解决。

My routes look like this: 我的路线如下所示:

routes: {
  '': 'index',
  'jobs/:id': 'viewJob',
  '*default': 'notFound'
}

It works fine when serving the app from the server root, but it doesn't when I serve it from a subfolder. 从服务器根目录提供应用程序时,它可以正常工作,但从子文件夹提供应用程序时,则不能。 In that case I always get the default route. 在那种情况下,我总是得到默认路由。

I though adding the root param to the backbone history would do the trick so I added it like this: 我虽然将参数添加到主干历史中就可以解决问题,所以我将其添加如下:

Backbone.history.start({ pushState: true, root: '/subdir/' });

With this the app seems to load as expected but the Router is automatically adding the root to all the routes and it ends up being duplicated, so when I first visit the website: 有了这个程序,应用程序似乎可以按预期加载,但是路由器会自动将添加到所有路由,并且最终被复制,因此当我第一次访问该网站时:

http://mysite.com/subdir/

It loads the app and changes it to this: 它加载应用程序并将其更改为:

http://mysite.com/subdir/subdir/

This makes the app to break when reloading the page or using the browser back button as that route doesn't really exist. 这会使应用程序在重新加载页面或使用浏览器后退按钮时中断,因为该路由实际上并不存在。

What would be the approach to avoid this? 避免这种情况的方法是什么? I don't want to hardcode the folder name in the routes as it might change or be served from the root. 我不想在路由中对文件夹名称进行硬编码,因为它可能会更改或从根目录提供。

UPDATE: I've just realized I was adding the duplicated folder name myself using router.navigate somewhere in my code. 更新:我刚刚意识到我自己使用router.navigate在代码中的某个位置添加了重复的文件夹名称。 I just removed it and everything works as expected. 我刚刚删除了它,一切正常。

Just a small guess.. do you really want pushState turned on? 只是一个小小的猜测..您真的要打开pushState吗? This can cause problems on reload if the server is not setup to serve out of the new directory. 如果未将服务器设置为不在新目录中提供服务,则这可能导致重新加载时出现问题。

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

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