简体   繁体   English

刷新具有多个'/'的路径会在Angular中产生错误

[英]Refresing a route with more than one '/' gives errors in Angular

Routes like /home , /profile , and even / work perfectly when pressing browser refresh button on them, because then, the server sends the default .html and angular files (because of the server code below), and these angular files then handle the current url. 当他们按下浏览器刷新按钮时,像/home/profile ,甚至/ work这样的路由完美,因为这样,服务器发送默认的.html和angular文件(因为下面的服务器代码),然后这些角文件处理目前的网址。

My problem occurs when I press refresh on routes with more than one / (which is necessary when using, say, route parameters), like /manage/users , /user/12345678 ...etc. 当我在具有多个/ (在使用路由参数时必需)的路由上刷新时会出现问题,例如/manage/users/user/12345678 ...等。

And then, jQuery throws this weird error Uncaught SyntaxError: Unexpected token < , and then Bootstrap starts complaining about not getting jQuery, and page looks crappy without the makeup ... and so on. 然后,jQuery抛出这个奇怪的错误Uncaught SyntaxError: Unexpected token < ,然后Bootstrap开始抱怨没有得到jQuery,并且页面看起来很糟糕没有化妆......等等。

Just to be clear, when I visit routes with more than one / , by using Angular's own router (using $state.go('/manage/users') , <ui-sref='profile'> etc.), this problem (obviously) doesn't occur, because then Angular has already been loaded to handle that url. 需要明确的是,当我访问多个/路由时,使用Angular自己的路由器(使用$state.go('/manage/users')<ui-sref='profile'>等),这个问题(显然)不会发生,因为Angular已经被加载来处理该URL。

But when I refresh the page, since there is no Angular to handle that route, browser asks the server for the route, and apparently, /* in Express can't handle this route too. 但是当我刷新页面时,由于没有Angular来处理该路由,浏览器会向服务器询问路由,显然,Express中的/*也无法处理此路由。

I serve the .html file from my server by: 我通过以下方式从我的服务器提供.html文件:

app.all('/*', function(req, res, next) {
    // Just send the index.html for other files to support HTML5Mode
    res.render('index.html');
});

after handling all the API routes. 处理完所有API路由后。 The index.html (hopefully) loads all the angular files afterwards. index.html(希望)之后加载所有角度文件。

Is there something like: 是否有类似的东西:

app.all('**/*', function(req, res, next) { ... });

... to render html at any route, with any number of / s? ...在任何路线上呈现html,任意数量的/ s? Or some kind of Regex maybe? 或者某种正则表达式可能吗?

TIA TIA

You have to set the base in your "head" 你必须在你的“头”中设置基数

<head>
  <base href="/">
  ...
</head>

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

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