【问题标题】:Distributed route definition in AngularJS [duplicate]AngularJS中的分布式路由定义[重复]
【发布时间】:2013-07-28 06:52:10
【问题描述】:

我一般知道AngularJS中的路由定义如下:

var app = angular.module('app', []);
app.config(function ($routeProvider) {
$routeProvider.when("/", { templateUrl: "/partials/home.html" }).
               when("/profile", { templateUrl: "/partials/profile.html" }).
               when("/contact", { templateUrl: "/partials/contact.html" }).
               otherwise({ redirectTo: '/' });
});

让我烦恼的是,我想使用 RequireJS 模块化我的应用程序,并且我想在需要它们的地方注册路由。例如,如果我有一个配置文件模块,我想从那里注册我的路线。同样适用于触点模块。这种集中化让我发疯。我是否在这里遗漏了一点,或者我的问题有什么好的解决方案?

【问题讨论】:

  • @Stewie 你说得对——我没找到这个

标签: angularjs routes requirejs route-provider


【解决方案1】:

您可以简单地将路由配置分布在您的模块上。您唯一必须做的就是在“app”模块中注册所有模块。

angular.module('app', ['app.profile', 'app.contact'])
  .config(function ($routeProvider) {
    $routeProvider.when("/", { templateUrl: "/partials/home.html" })
                  .otherwise({ redirectTo: '/' });
  });

angular.module('app.profile', [])
  .config(function ($routeProvider) {
    $routeProvider.when("/profile", { templateUrl: "/partials/profile.html" });
  });

angular.module('app.contact', [])
  .config(function ($routeProvider) {
    $routeProvider.when("/contact", { templateUrl: "/partials/contact.html" });
  });

【讨论】:

  • 这很有意义 - 谢谢!
【解决方案2】:

您不必在一处完成 $routeProvider 的配置。根据需要添加路由定义,即当您知道您将使用例如配置文件模块时,您可以添加与其路由对应的 .when() 子句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    相关资源
    最近更新 更多