简体   繁体   English

angular等于angularjs状态

[英]angular's equivalent to angularjs states

In AngularJS, for routing purposes, we define states with children which makes it possible to switch between views with the result that each view is always rendered in one container: 在AngularJS中,出于路由目的,我们使用子节点定义状态,这使得可以在视图之间切换,结果是每个视图始终在一个容器中呈现:

$stateProvider.state("communication.create-message", {
    url: ...,
    templateUrl: ...,
    menu: ...,
    parent: "communication",
    ncyBreadcrumb: {
        label: "Create Message"
    }
});

Whichever state we choose - the view is always rendered within one container that has ui-view attribute. 无论我们选择哪种状态 - 视图总是在一个具有ui-view属性的容器中呈现。

I'm trying to achieve the same in Angular 2 or above , but I have no idea of how to reproduce the above-stated functionality. 我试图在Angular 2或更高版本中实现相同,但我不知道如何重现上述功能。

In app.component.ts we have router-outlet where component templates get rendered. 在app.component.ts中,我们有router-outlet ,其中组件模板被渲染。

Say, we have many nested child routes - is it possible to render all of them within this outlet ? 比如说,我们有许多嵌套的子路径 - 是否有可能在这个出口内渲染所有这些路径?

What would the code in app-routing.module.ts look like in this case ? 在这种情况下, app-routing.module.ts的代码会是什么样子?

Could anyone please give a working example of how to go about it ? 有谁能请举例说明如何去做?

Step 1 : Import Routes from @angular/router in app.module.ts .. import Routes. 步骤1: app.module.ts ..导入路由中的@ angular / router导入路由。 You have to write 你必须写

import {Routes} from '@angular/core' 

Step 2 : Add all the routes you want to set up in an array pf type Routes like : this is for informing angular all the routes in your app. 步骤2: 添加要在数组中设置的所有路由pf类型路由如下 :这是用于通知角度应用中的所有路由。 Each route is a javascript object in this array. 每个路由都是此数组中的javascript对象。

const appRoutes : Routes = [
  {path : 'communication-route'} 
]

always you have to give path , this what you enter after your domain like "localhost :4200/communication-route". 总是你必须提供路径,这就是你在域名之后输入的内容,如“localhost:4200 / communication-route”。

Step 3: Add the action to route ie what happens when this path is reached. 步骤3: 添加行动路线,即达到此路径时会发生什么。

 const appRoutes : Routes = [
  {path : 'communication-route'} , component :communicationroutecomponent
 ]

here i have given the component name "communicationroutecomponent" , ie this component will be loaded when the path "/communication-route" is reached 这里我给出了组件名称“communicationroutecomponent”,即当到达路径“/ communication-route”时将加载该组件

Step 4: Register your routes in your app To do this you will have to do new import in app.module.ts 第4步: 在您的应用程序中注册您的路线要执行此操作,您必须在app.module.ts中执行新的导入

import {RouterModule} from '@angular/router'

Routermodule has special method forRoot() which registers our routes . Routermodule有一个特殊的方法forRoot()来注册我们的路由。

In our case we will have to write this piece of code in 在我们的例子中,我们将不得不编写这段代码

imports :[
 RouterModule.forRoot(appRoutes)
]

Our routes are now registered and angular knows our routes now. 我们的路线现已注册,而且我们现在知道我们的路线。

Step 5 : Where to display the route content ie the html content of you route page. 步骤5: 在哪里显示路由内容,即路由页面的html内容。

For this angular has directive . 对于这个angular has指令。 We need to include where we want to load our content ie in the html. 我们需要包含我们想要加载内容的位置,即在html中。

<a router-outlet="/communication-route"></a>

Navigating to routes : angular gives a directive for this routerLink so if we want to navigate to users component , you can give this in your html element: 导航到路径: angular为此routerLink提供了一个指令,因此如果我们要导航到用户组件,您可以在html元素中指定:

routerLink="/communication-route" routerLink =“/通信路径”

I hope i was able to explain how this works. 我希望我能够解释这是如何工作的。

Your code should be as follows 您的代码应如下所示

    export const ComRoute: Routes = [
{
path: 'communication-route',
children: [
{ path: 'communication', component: communicationComponent },
{ path: ':child1', component: child1Component },
{ path: ':child1/field', component: child1Component}
]
}
];

First of all, states are not an official AngularJS concept. 首先,州不是官方的AngularJS概念。 They come from ui-router, which began life as an alternate to the simplistic built in router. 它们来自ui-router,它开始作为简单的内置路由器的替代品。

Eventually, ui-router became the de facto standard for routing in AngularJS while the official ng-route module was extracted into a separate, optional library by the Angular team. 最终,ui-router成为AngularJS中路由的事实标准,而官方ng-route模块被Angular团队提取到一个单独的可选库中。

ui-router, is complex but exceptional and has earned what is in my view well deserved popularity and success. ui-router,虽然很复杂但很特别,并且在我看来获得了当之无愧的人气和成功。 This success has led to its expansion to support additional platforms enabling the same powerful state based structure in applications written for frameworks such as React. 这一成功促使其扩展以支持其他平台,这些平台在为React等框架编写的应用程序中实现了同样强大的基于状态的结构。

It is now available for Angular (formerly known as Angular 2). 它现在可用于Angular (以前称为Angular 2)。

You can read the documentation and see how to get started on https://ui-router.github.io/ng2 您可以阅读文档,了解如何开始使用https://ui-router.github.io/ng2

Here is a snippet from the src/app/app.states.ts module of the official example repository 这是来自官方示例存储库src/app/app.states.ts模块的片段

export const loginState = {
  parent: 'app',
  name: 'login',
  url: '/login',
  component: LoginComponent,
  resolve: [
    { token: 'returnTo', deps: [Transition], resolveFn: returnTo },
  ]
};

As we can see, there are compatible concepts available, including what looks like a nice evolution of the resolves API which allows function oriented injection which was generally simpler than class based injection in ui-router with AngularJS . 正如我们所看到的,存在可用的兼容概念,包括看起来像解析API的一个很好的演变,它允许面向函数的注入,这通常比使用AngularJS的 ui-router中的基于类的注入更简单。

Note, I have not used it in an Angular project. 注意,我没有在Angular项目中使用过它。

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

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