【问题标题】:Angular Js routing using $routeProviderAngular Js 路由使用 $routeProvider
【发布时间】:2018-05-05 21:41:05
【问题描述】:

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

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

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

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

【问题讨论】:

  • 您需要为 html5Mode 配置服务器或在 Angular 应用程序中使用基于哈希的 url。这是您看到的服务器端错误

标签: angularjs


【解决方案1】:

试试这个

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);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-05
    • 2016-01-27
    • 2013-10-19
    • 2015-09-05
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 2019-10-24
    相关资源
    最近更新 更多