简体   繁体   English

angular-route 和 angular-ui-router 有什么区别?

[英]What is the difference between angular-route and angular-ui-router?

I'm planning to use AngularJS in my big applications.我计划在我的大型应用程序中使用AngularJS So I'm in the process to find out the right modules to use.所以我正在寻找要使用的正确模块。

What is the difference between ngRoute (angular-route.js) and ui-router (angular-ui-router.js) modules? ngRoute (angular-route.js)ui-router (angular-ui-router.js)模块有什么区别

In many articles when ngRoute is used, route is configured with $routeProvider .在很多使用ngRoute 的文章中,路由是通过$routeProvider配置的。 However, when used with ui-router , route is configured with $stateProvider and $urlRouterProvider .但是,当与ui-router 一起使用时,路由配置为$stateProvider 和 $urlRouterProvider

Which module should I use for better manageability and extensibility?我应该使用哪个模块以获得更好的可管理性和可扩展性?

ui-router is a 3rd-party module and is very powerful. ui-router是一个第三方模块,非常强大。 It supports everything the normal ngRoute can do as well as many extra functions.它支持普通 ngRoute 可以做的一切以及许多额外的功能。

Here are some common reason ui-router is chosen over ngRoute:以下是选择 ui-router 而不是 ngRoute 的一些常见原因:

  • ui-router allows for nested views and multiple named views . ui-router 允许嵌套视图多个命名视图 This is very useful with larger app where you may have pages that inherit from other sections.这对于较大的应用程序非常有用,其中您可能有从其他部分继承的页面。

  • ui-router allows for you to have strong-type linking between states based on state names. ui-router 允许您根据状态名称在状态之间建立强类型链接。 Change the url in one place will update every link to that state when you build your links with ui-sref .当您使用ui-sref构建链接时,在一处更改 url 会将每个链接更新为该状态。 Very useful for larger projects where URLs might change.对于 URL 可能会更改的大型项目非常有用。

  • There is also the concept of the decorator which could be used to allow your routes to be dynamically created based on the URL that is trying to be accessed.还有装饰器的概念,可用于允许基于尝试访问的 URL 动态创建您的路由。 This could mean that you will not need to specify all of your routes before hand.这可能意味着您无需事先指定所有路线。

  • states allow you to map and access different information about different states and you can easily pass information between states via $stateParams . 状态允许您映射和访问有关不同状态的不同信息,并且您可以通过$stateParams轻松地在状态之间传递信息。

  • You can easily determine if you are in a state or parent of a state to adjust UI element (highlighting the navigation of the current state) within your templates via $state provided by ui-router which you can expose via setting it in $rootScope on run .您可以通过 ui-router 提供的$state轻松确定您是处于某个状态还是某个状态的父级以调整模板中的 UI 元素(突出显示当前状态的导航),您可以通过在$rootScope上设置它来公开它run

In essence, ui-router is ngRouter with more features, under the sheets it is quite different. ui-router本质上是ngRouter,功能比较多,在sheet下就大不一样了。 These additional features are very useful for larger applications.这些附加功能对于大型应用程序非常有用。

More Information:更多信息:

ngRoute is a module developed by the AngularJS team which was earlier part of the AngularJS core. ngRoute是 AngularJS 团队开发的一个模块,它是 AngularJS 核心的早期部分。

ui-router is a framework which was made outside the AngularJS project to improve and enhance routing capabilities. ui-router是一个框架,它是在 AngularJS 项目之外制作的,用于改进和增强路由功能。

From the ui-router documentation :来自 ui-router文档

AngularUI Router is a routing framework for AngularJS, which allows you to organize the parts of your interface into a state machine. AngularUI Router 是 AngularJS 的路由框架,它允许您将界面的各个部分组织成一个状态机。 Unlike the $route service in Angular core, which is organized around URL routes, UI-Router is organized around states, which may optionally have routes, as well as other behavior, attached.与 Angular 核心中的 $route 服务不同,它是围绕 URL 路由组织的,UI-Router 是围绕状态组织的,它可以选择附加路由以及其他行为。

States are bound to named, nested and parallel views, allowing you to powerfully manage your application's interface.状态绑定到命名的、嵌套的和并行的视图,使您能够有力地管理应用程序的界面。

Neither of them is better, you will have to chose the most appropriate for your project.它们都不是更好,您必须选择最适合您的项目的。

However, if you plan to have complex views in your application and you would like to deal with the "$state" notion.但是,如果您计划在您的应用程序中使用复杂的视图并且您想要处理“$state”概念。 I recommend you to chose ui-router.我建议你选择 ui-router。

ngRoute is a angular core module which is good for basic scenarios. ngRoute 是一个 angular 核心模块,适用于基本场景。 I believe that they will add more powerful features in upcoming releases.我相信他们会在即将发布的版本中添加更强大的功能。

URL: https://docs.angularjs.org/api/ngRoute网址: https : //docs.angularjs.org/api/ngRoute

Ui-router is a contributed module which is overcome the problems of ngRoute. Ui-router 是一个贡献模块,它克服了 ngRoute 的问题。 Mainly Nested/Complex views.主要是嵌套/复杂视图。

URL: https://github.com/angular-ui/ui-router网址: https : //github.com/angular-ui/ui-router

Some of the difference between ui-router and ngRoute ui-router 和 ngRoute 的一些区别

http://www.amasik.com/angularjs-ngroute-vs-ui-router/ http://www.amasik.com/angularjs-ngroute-vs-ui-router/

在此处输入图片说明

ngRoute is part of the core AngularJS framework. ngRoute 是核心 AngularJS 框架的一部分。

ui-router is a community library that has been created to attempt to improve upon the default routing capabilities. ui-router 是一个社区库,旨在尝试改进默认路由功能。

Here is a good article about configuring/setting up ui-router:这是一篇关于配置/设置 ui-router 的好文章:

http://www.ng-newsletter.com/posts/angular-ui-router.html http://www.ng-newsletter.com/posts/angular-ui-router.html

如果您想利用在 ngRoute 范式中实现的嵌套视图功能,请尝试angular-route-segment - 它旨在扩展 ngRoute 而不是替换它。

Generally ui-router works on a state mechanism... It can be understood with an easy example:一般ui-router是工作在状态机制上的…… 举个简单的例子就可以理解:

Let's say we have a big application of a music library (like ..gaana or saavan or any other).假设我们有一个音乐库的大型应用程序(如 ..gaana 或 saavan 或任何其他)。 And at the bottom of the page, you have a music player which is shared across all the state of the page.在页面底部,您有一个音乐播放器,它在页面的所有状态中共享。

Now let's say you just click on some songs to play.现在假设您只需单击一些歌曲即可播放。 In this case, only that music player state should change instead of reloading the full page.在这种情况下,只有那个音乐播放器状态应该改变而不是重新加载整个页面。 That can be easily handled by ui-router.这可以通过 ui-router 轻松处理。

While in ngRoute we just attach the view and the controller.在 ngRoute 中,我们只附加了视图和控制器。

Angular 1.x角度 1.x

ng-route : ng-路由

ng-route is developed by the angularJS Team for routing. ng-route 由 angularJS 团队开发,用于路由。

ng-route: url (Location) based routing. ng-route:基于 url(位置)的路由。

Ex:前任:

$routeProvider
    .when("/home", {
        templateUrl : "home.html"
    })

ui-route :用户界面路由

ui-router is develoepd by 3rd party module. ui-router 是由 3rd 方模块开发的。

ui-router : state based routing ui-router :基于状态的路由

Ex:前任:

 $stateProvider
        .state('home', {
            url: '/home',
            templateUrl: 'home.html'
        })

--> ui-router allows for nested views --> ui-router 允许嵌套视图

--> ui-router more powerful than ng-route --> ui-router 比 ng-route 更强大

ng-router , ui-router ng-router , ui-router

ngRoute is a module built by the Angular team that provides basic client-side routing functionality. ngRoute 是 Angular 团队构建的一个模块,提供基本的客户端路由功能。 This module provides a fairly powerful base for routing, and can be built upon pretty easily to give solid routing functionality, as exemplified in this blog post (be sure to read the comment trail between Ward Bell and Ben Nadel, the author - they are a couple of Angular pros)该模块为路由提供了一个相当强大的基础,并且可以很容易地构建以提供可靠的路由功能,如本博客文章中所示(请务必阅读作者 Ward Bell 和 Ben Nadel 之间的评论线索 - 他们是几个 Angular 专家)

ui-router shifts the focus from url-centric routes to application "states", which may or may not be reflected in the url. ui-router 将焦点从以 url 为中心的路由转移到应用程序“状态”,这可能会或可能不会反映在 url 中。

The primary features added by ui-router are nested states and named views. ui-router 添加的主要功能是嵌套状态和命名视图。

Nested states allow you to separate controller logic for the various pieces of the application.嵌套状态允许您为应用程序的各个部分分离控制器逻辑。 A very simple example of this would be an app with primary navigation across the top, a secondary navigation list along the left, and content on the right.一个非常简单的例子是一个应用程序,其顶部有主导航,左侧有辅助导航列表,右侧有内容。 Without nested states, a single controller would typically have to handle the display logic for the secondary navigation as well as the content.如果没有嵌套状态,单个控制器通常必须处理辅助导航和内容的显示逻辑。 Nested routing allows you to separate these concerns.嵌套路由允许您分离这些问题。

Named views are another additional feature of ui-router.命名视图是 ui-router 的另一个附加功能。 With ngRoute, you can only have a single ngView directive on a page, whereas with named views in ui-router you can specify multiple ui-view directives, and then each state is able to affect the template and controller of the names views.使用 ngRoute,您只能在页面上有一个 ngView 指令,而使用 ui-router 中的命名视图,您可以指定多个 ui-view 指令,然后每个状态都能够影响名称视图的模板和控制器。 A super simple example of this would be to have the main content of your app be the primary view, and then to also have a footer bar that would be a separate ui-view.一个超级简单的例子是让你的应用程序的主要内容成为主视图,然后还有一个页脚栏作为一个单独的 ui 视图。 In this scenario, the footer's controller no longer has to listen for state/route changes.在这种情况下,页脚的控制器不再需要监听状态/路由的变化。

A good comparison of ngRoute and ui-router can be found on this podcast episode. ngRoute 和 ui-router 的一个很好的比较可以在这个播客节目中找到。

Just to make things more confusing, keep an eye on the new "official" routing module that the Angular team is expecting to release for versions 1.5 and 2.0 of Angular.为了让事情变得更加混乱,请密切关注 Angular 团队希望为 Angular 1.5 和 2.0 版本发布的新“官方”路由模块。 This will be replacing the ngRoute module.这将替换 ngRoute 模块。 Here is the current documentation for the new Router module - it is fairly sparse as of this posting since the implementation has not yet been finalized.是新路由器模块的当前文档 - 由于实施尚未最终确定,因此在本文发布时相当稀疏。 Watch here for more news on when this module will actually be released.在此处观看有关此模块何时实际发布的更多新闻。

ngRoute is a basic routing library, where you can specify just one view and controller for any route. ngRoute 是一个基本的路由库,您可以在其中为任何路由指定一个视图和控制器。

With ui-router, you can specify multiple views, both parallel and nested.使用 ui-router,您可以指定多个视图,包括并行视图和嵌套视图。 So if your application requires (or may require in future) any kind of complex routing/views, then go ahead with ui-router.因此,如果您的应用程序需要(或将来可能需要)任何类型的复杂路由/视图,请继续使用 ui-router。

This is best getting started guide for AngularUI Router.是 AngularUI 路由器的最佳入门指南。

Basic thing you have to know: ng-router uses $location.path() and ui-router uses $state.go你必须知道的基本事情:ng-router 使用$location.path()而 ui-router 使用$state.go

Rest us all features.休息我们所有的功能。

ui router make your life easier! ui路由器让您的生活更轻松! You can add it to you AngularJS application via injecting it into your applications...您可以通过将它注入到您的应用程序中来将它添加到您的 AngularJS 应用程序中...

ng-route comes as part of the core AngularJS, so it's simpler and gives you fewer options... ng-route是 AngularJS 核心的一部分,所以它更简单,给你的选择更少......

Look at here to understand ng-route better: https://docs.angularjs.org/api/ngRoute查看此处以更好地了解 ng-route: https : //docs.angularjs.org/api/ngRoute

Also when using it, don't forget to use: ngView ..同样在使用它时,不要忘记使用: ngView ..

ng-ui-router is different but: ng-ui-router 不同,但:

https://github.com/angular-ui/ui-router but gives you more options.... https://github.com/angular-ui/ui-router但为您提供了更多选择....

AngularUI Router is a routing framework for AngularJS, which allows you to organize the parts of your interface into a state machine. AngularUI Router 是 AngularJS 的路由框架,它允许您将界面的各个部分组织成一个状态机。 Unlike the $route service in the Angular ngRoute module, which is organized around URL routes, UI-Router is organized around states, which may optionally have routes, as well as other behavior, attached.与 Angular ngRoute 模块中的 $route 服务不同,它是围绕 URL 路由组织的,UI-Router 是围绕状态组织的,它可以选择性地附加路由以及其他行为。

https://github.com/angular-ui/ui-router https://github.com/angular-ui/ui-router

ngRoute is a module developed by the Angular.js team which was earlier part of the Angular core. ngRoute 是 Angular.js 团队开发的一个模块,它是 Angular 核心的早期部分。

ui-router is a framework which was made outside the Angular.js project to improve and enhance routing capabalities. ui-router 是一个框架,它是在 Angular.js 项目之外制作的,用于改进和增强路由功能。

1- ngRoute is developed by angular team whereas ui-router is a 3rd party module. 1- ngRoute 由 angular 团队开发,而 ui-router 是第 3 方模块。 2- ngRoute implements routing based on the route URL whereas ui-router implements routing based on the state of the application. 2- ngRoute 基于路由 URL 实现路由,而 ui-router 基于应用程序状态实现路由。 3- ui-router provides everything that the ng-route provides plus some additional features like nested states and multiple named views. 3- ui-router 提供了 ng-route 提供的所有内容以及一些附加功能,例如嵌套状态和多个命名视图。

ng-View (developed by the AngularJS team) can be used only once per page, whereas ui-View (3rd party module) can be used multiple times per page. ng-View (由 AngularJS 团队开发)每页只能使用一次,而ui-View (第三方模块)每页可以使用多次。

ui-View is therefore the best option.因此ui-View是最好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM