Angular Js使用$ routeProvider进行路由

时间:2018-05-05 13:17:53

标签: angularjs

我正在使用$ routeProvider使用angular js路由,但是当我重新加载页面时,我收到错误消息,例如“Can not GET / home”。我的代码如下

.when("/", {
  templateUrl: "views/login/login.html",
  controller: "LoginController as loginCtrl"
})

.when("/home", {
  templateUrl: "views/home/home_template.html",
  controller: "HomeController as homeCtrl"
});

当我从家里重新加载页面时,我收到此错误消息。此外,我希望将此登录保留在路由之外,因此我应该遵循什么做法

1 个答案:

答案 0 :(得分:0)

试试这个

var app = angular.module('app', [
    'ngRoute'
  ]);

window.routes =
    {
        '/': {
            templateUrl: 'app/visitor/visitor.html',
            anonymous: true
        },
        '/index': {
            templateUrl: 'app/visitor/visitor.html',
            caseInsensitiveMatch: true,
            anonymous: true
        },
        '/lu/:mt?/:tab?/:id?': {
            templateUrl: 'app/user/user.html',
            caseInsensitiveMatch: true,
            anonymous: false
        }
    };

app.config(function ($routeProvider, $locationProvider) {
    for (var path in window.routes) {
      $routeProvider.when(path, window.routes[path]);
    }
    $routeProvider.otherwise({redirectTo: '/'});

    $locationProvider.html5Mode(true);
});
相关问题