我正在使用 AngularJS 编写一个 SPA 产品,由 Express 使用 NodeJS 提供服务。 NodeJS/Express 还提供了 SPA 的 API。在生产中,SPA 将位于代理(Apache,或者将来可能是 nginx)后面 - 即使没有代理,以下问题也存在。 ngRoute用于提供路由。
我最初在本地测试该应用程序,一切正常。我将存储库克隆到测试服务器,但由于某种原因,当我尝试使用该应用程序时,URL 格式错误。
通常,当页面加载时,应用程序会重定向到 localhost:8080/#/home
( home
是包罗万象的路线)。但是,在测试服务器上时,它会重定向到 domain.com/#!/home#%2Fhome
.
如果我单击一个链接,则应将 URL 更改为 domain.com/#/quotations
(href 设置为相对的,而不是绝对的),我得到 domain.com/#!/home#%2Fquotations
.
我的路由器配置:
app.config(['$routeProvider', function($routeProvider){
// Routes
$routeProvider
.when('/home', {
templateUrl: '/views/home/home.html'
})
.when('/staff', {
templateUrl: '/module/staff/views/staff.html',
controller: 'staffController',
controllerAs: 'staff'
})
.when('/error', {
templateUrl: '/views/error/error.html',
css: '/views/error/error.css'
})
.otherwise({
redirectTo: '/home'
});
}]);
在我的主 NodeJS 服务器文件中,我仅使用 web.use(Express.static('public_html'))
设置 Express 为文件夹 public_html
提供服务包含 SPA。
我尝试添加 <base href="/">
进入我的index.html的头部,但这没有什么区别。 Angular 和 Express 都没有配置为执行任何与默认不同的操作来重写页面 URL,因此我对问题所在感到非常困惑。
最佳答案
在 Angular 1.6 中,$location 现在添加了一个“!”如果你想删除它,你可以这样做:
appModule.config(['$locationProvider', function($locationProvider) {
$locationProvider.hashPrefix('');
}]);
检查这个问题,可能会有所帮助... Angular web app having extra ! in the url
关于javascript - Angular ngRoute 重定向到格式错误的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41134597/