我正在尝试使用 locationProvider 从我的 Angularjs 应用程序的 URL 中删除主题标签,并且在我手动刷新页面之前它效果很好。这总是会导致浏览器中出现“cannot GET ..”错误。我做了一些研究,我认为我必须使用 .htaccess 文件来重定向,但我不确定如何实现它,并且我已经在网上搜索了解决方案,但到目前为止还没有运气。
这是我的 app.js,它处理到我的 Angular View 和 Controller 的路由:
(function () {
'use strict';
var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(function($routeProvider, $locationProvider) {
$routeProvider.
when('/', {
templateUrl: 'views/partials/play.html',
controller: 'playCtrl'
}).
when('/dictionary', {
templateUrl: 'views/partials/dictionary.html',
controller: 'dictionaryCtrl'
}).
when('/add', {
templateUrl: 'views/partials/word.html',
controller: 'wordCtrl'
}).
when('/about', {
templateUrl: 'views/partials/about.html',
controller: 'aboutCtrl'
}).
otherwise({redirectTo: '/'});
// use the HTML5 History API
$locationProvider.html5Mode(true);
});
})();
我还在 <head>
中添加了这个在我的index.html中:
<base href="/">
如果有帮助的话,该应用程序是使用 Node.js、Express、Angular 和 Mongodb 构建的。
非常感谢。
最佳答案
如果其他人想知道您需要将 Node 未使用的所有路由重定向到 Angular:
app.get('*', function(req, res) {
res.sendfile('./path/to/index.html')
})
这必须在 server.js 中完成。不要忘记在 sendfile 链接中使用 ./:)
关于javascript - $locationProvider html5mode 导致刷新时出现 "cannot GET"错误 - AngularJS Node 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36634280/