不同文件中的Angularjs路由 [英] Angularjs routing in different files

查看:31
本文介绍了不同文件中的Angularjs路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查角度路由.

http://www.bennadel.com/blog/2420-Mapping-AngularJS-Routes-Onto-URL-Parameters-And-Client-Side-Events.htm

我看到的示例将所有路由都定义在同一个文件中.如何在不同的文件/模块中定义各种路由?

The examples I see have all the routes be defined in the same file. How do I have various routes be defined in different files / modules?

推荐答案

在 AngularJS 中,路由是在配置块中定义的.每个 AngularJS 模块可以有多个配置块,您可以在每个配置块中定义路由.整个应用的最终路由是所有模块中定义的路由的总和.

In AngularJS routes are defined in configuration block. Each AngularJS module can have multiple configuration blocks and you can define routes in each and every configuration block. The final routing for the entire application is a sum of routes defined in all modules.

在实践中你可以这样做:

In practice you can do it like:

angular.module('myModule1', []).config(function($routeProvider){
  //define module-specific routes here
});

angular.module('myModule2', []).config(function($routeProvider){
  //define module-specific routes here
});

angular.module('myApp', ['myModule1', 'myModule2']).config(function($routeProvider){
  //define app-level routes here
});

关于拆分文件 - 我想这在很大程度上取决于您如何拆分文件中的 AngularJS 模块.我的建议是坚持一个文件等于一个模块的原则.

Regarding the split over files - I guess this largely depends on how you split AngularJS modules in files. What I would recommend is sticking to one-file equals one-module principle.

您可以在 angular-app 中看到所有这些都应用于更大规模的 Web 应用程序,这是为用 AngularJS 编写的非平凡应用程序构建参考的努力:

You can see all this applied to a larger-scale web application in angular-app, an effort to build a reference for a non-trivial application written in AngularJS:

在提到的应用程序中,您可以看到在多个文件中定义的路由,例如:

In the mentioned app you can see routes defined in multiple files, ex.:

这篇关于不同文件中的Angularjs路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆