我们使用 AngularJS 版本 1.3.16 和 nodeJS 作为后端,ng-route 用于角度路由。工作代码由 #! 作为节点和角度的 URL 分隔符。
示例 URL:/store/1234/#!/department/produce /store/1234/#!/department/produce/category/fruits
NodeJS 路由代码:
app.get('/store/:storeid', ctrl.storeView);
角度路由代码:
$routeProvider.when('/department/:deptIndex', {
controller: 'CartController',
resolve: {
// I will cause a 1 second delay
delay: function ($q, $timeout) {
var delay = $q.defer();
$timeout(delay.resolve, 1000);
return delay.promise;
}
}
}).when('/department/:deptIndex/category/:catIndex', {
controller: 'CartController',
resolve: {
// I will cause a 1 second delay
delay: function ($q, $timeout) {
var delay = $q.defer();
$timeout(delay.resolve, 1000);
return delay.promise;
}
}
});
$locationProvider.html5Mode(false).hashPrefix('!');
为了使 URL 的 SEO 友好和可抓取,我们必须从 URL 中删除 hashbang。因此,当我们尝试启用 html5 模式时,就会出现问题。启用该模式后,角度路由不起作用。