我有一个 angular-sails 应用程序,我想在其中从 url 中删除散列
我将此添加到我的客户端 app.config
$locationProvider.html5Mode(true);
路由一直有效,直到我刷新页面。
导航到 http://localhost:1337/about
作品。我刷新得到
{
"status": 404
}
来自风 sails 服务器。如何配置 sails 以使用 angularjs html5Mode?p>
最佳答案
要使用 HTML5Mode,您需要将服务器设置为仅提供您的根(或“index.html”)文件。 “web.com/index.html”。在我看来,最好将您的 Sails 应用程序和您的 Angular 前端分开以单独交付。这样你就可以轻松地设置为你的 Angular 客户端前端提供服务的 web 服务器,以便仅在非 AJAX 调用上提供 index.html。
要在 sails 中执行此操作没有“开关”,您可以设置一个应用程序范围的策略来检查是 angular 正在调用还是浏览器正在调用并发送适当的文件,您可以看到下面的代码。您必须确保 Angular 正在设置以下标题 X-Requested-With = XMLHttpRequest
应用政策
module.exports = function(req, res, next) {
// Checks if this is an ajax request
// If True then skip this and return requested content
// If False then return the index (or root) page
if (if !req.isAjax) {
return res.view("/index");
}
return next();
};
关于javascript - 配置 sails 服务器以使用 html5Mode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24637800/