0

我们使用 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 模式时,就会出现问题。启用该模式后,角度路由不起作用。

4

3 回答 3

1

您是否在 index.html 中设置了基本网址?

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

您还需要将 html5mode 设置为 true 并删除 hashPrefix

$locationProvider.html5Mode(true);
于 2017-03-21T05:29:39.553 回答
0

每个路由都应该有一个模板或一个模板 URL ,并且你的 HTML 应该有 ng-view 来查看路由内容

于 2017-03-21T03:20:43.610 回答
0

一旦我将基本 href 更新为节点服务器 url,我的路由就开始工作:

<head> <base href='/store/'+store.storeCode> </head>

我还更新了以商店代码为前缀的角度路由 url,例如:

app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
$routeProvider.when('/:storeid/department/:deptIndex', {
    //my routing code
});
于 2017-03-22T20:09:03.123 回答