标签: angular2-routing

将属性添加到 angular2 中的路由

我想知道是否可以向 RouteConfig 装饰器中定义的路由添加自定义属性。

我想添加任何需要身份验证的路由:

@RouteConfig([
  {
    path: "/login",
    name: "Login",
    component: LoginComponent,
    authenticate: false,
  },
  {
    path: "/home",
    name: "Home",
    component: HomeComponent,
    authenticate: true
  }
])
Run Code Online (Sandbox Code Playgroud)

如果这是可能的,我如何在组件中获取值“authenticate”?

angular2-routing angular

3
推荐指数
1
解决办法
2499
查看次数

angular2 中不再存在的 routerState.parent 的替代方案

在 angular2 rc5 中,我有这个代码。

this.router.routerState.parent(this.route).params.forEach((params: Params) => {
    url = params['url'];
    id = +params['id'];
});
Run Code Online (Sandbox Code Playgroud)

我不得不使用它,因为我在我的路由器上低了两个级别,并且由于某种原因这样做不起作用。

this.route.params.forEach((params: Params) => {
        url = params['url'];
        id = +params['id'];
    });
Run Code Online (Sandbox Code Playgroud)

我仍然很乐意这样做,但是在 angular2 中,routerState 上.parent不再存在该方法。

我可以实现我想要的,但使用 angular 2.0.0 中可用的不同方法吗?

html javascript angularjs angular2-routing angular

3
推荐指数
1
解决办法
995
查看次数

加载第一个子路由后,Angular2路由无法正常工作

我的路由配置方式如下:

RouterModule.forRoot([
    { path: '', redirectTo: 'main', pathMatch: 'full' },
    { path: 'main', component: SummaryComponent, children: [
        { path: 'data/:deviceId/:incidentId', component: DeviceMetricsComponent },
        { path: 'incident/analysis/:incidentId', component: IncidentAnalysisComponent },
      ] 
    },
    { path: 'test', component: TestFullPageComponent, children: [
        { path: 'test2', component: TestFullPageChildComponent}
      ] 
    },

    { path: 'logs/jobs/:jobtype', component: JobLogsComponent, pathMatch: 'full' },
    { path: '**', redirectTo: 'main' }
])
Run Code Online (Sandbox Code Playgroud)

这是我的"主要"模板:

<div class="row">
    <div class="col-md-12">
        <!-- Some links are in this table that load into the router-outlet below -->
        <app-incident-node-table …
Run Code Online (Sandbox Code Playgroud)

angular2-routing angular

3
推荐指数
1
解决办法
785
查看次数

使用Angular路由器切换并保留ng2-bootstrap <tabset>,<tab>的选定选项卡

我有一个组件显示使用ng2-bootstrap tabsettab.

例:

<tabset>
    <tab heading="Info" [active]="tabs[0].active">
        <account-data *ngIf="tabs[0].active"></account-data>
    </tab>
    <tab heading="Users" [active]="tabs[1].active">
        <manage-users *ngIf="tabs[1].active"></manage-users>
    </tab>
    <tab heading="Billing" [active]="tabs[2].active">
        <account-billing *ngIf="tabs[2].active"></account-billing>
    </tab>
</tabset>
Run Code Online (Sandbox Code Playgroud)

注意:tabs[N].active由组件控制并已同步选项卡更改和路由.但我觉得我的做法是错误的,因为在选定的标签内部管理路由很困难.让我们看第二个标签,在一天结束时它应该管理以下子路径:

.../users          -> provide list of users
.../users/new      -> create new user
.../users/:id      -> show a particular user
.../users/:id/edit -> edit a particular user
Run Code Online (Sandbox Code Playgroud)

这并不容易,因为显示标签的组件已经使用了这条路线:

.../:tab
Run Code Online (Sandbox Code Playgroud)

如果有这样的事情会更容易:

<tabset>
    <tab heading="Info" [routerLink]="['info']"></tab>
    <tab heading="Users" [routerLink]="['users']"></tab>
    <tab heading="Billing" [routerLink]="['billing']"></tab>
</tabset>
<router-outlet></router-outlet>
Run Code Online (Sandbox Code Playgroud)

有人为此解决了吗?这个问题应该很常见......

angular2-routing ng2-bootstrap angular2-router angular

3
推荐指数
1
解决办法
6807
查看次数

Angular 2/4中的嵌套路由

我在一个应用程序上工作,我打算有以下结构:

-MAIN -  main “container” (main routes)    
--NCF (lazy loaded, routes for it’s subapps)    
---ACNP (lazy loaded)    
----Component1    
----Component2    
---SubApp2 (lazy loaded)    
---SubApp3 (lazy loaded)    
--App2 (lazy loaded)    
--App3 (lazy loaded)    
--…
Run Code Online (Sandbox Code Playgroud)

这是应用程序的起始框架,它将有3个级别 - 主要,应用程序和子应用程序.每个应用程序和子应用程序都是独立开发的.将来可能会有更多延迟加载的级别.我希望在每个级别上独立维护路由,这意味着MAIN将处理NCF和App2和App3的路由(主要是延迟加载); NCF将处理ACNP,SubApp2和SubApp3的路由(再次大多是延迟加载); ACNP将处理其组件的路由.以下是它现在的样子:

MAIN-routes.ts:

import {Routes} from "@angular/router"

export const appRoutes: Routes = [
  {
    path: 'ncf',
    loadChildren: './ncf/ncf.module#NewCustomerFolderModule
  }
]
Run Code Online (Sandbox Code Playgroud)

main.html中

<h1>FE2</h1>
<MAIN-nav></MAIN-nav>
<router-outlet></router-outlet>
Run Code Online (Sandbox Code Playgroud)

MAIN-NAV

<a [routerLink]="['/ncf']">New Customer Folder</a>
Run Code Online (Sandbox Code Playgroud)

它工作正常,在MAIN-nav内部我有链接懒惰加载NCFModule.

NCFModule.ts

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(ncfRoutes)
  ],
  declarations: [NewCustomerFolderComponent]
})
Run Code Online (Sandbox Code Playgroud)

ncfRoutes.ts

import {Routes} from "@angular/router" …
Run Code Online (Sandbox Code Playgroud)

angular2-routing angular

3
推荐指数
1
解决办法
7347
查看次数

Angular 2 Router链接处于活动状态,两个链接处于激活状态

我有一个具有以下路线的项目:

{ path: '', component: GamesListingComponent },

    { path: 'inicio', component: HomeComponent },

    { path: 'listar',
        children: [
            { path: '', component: HomeComponent },
            { path: 'fabricantes', component: ManufacturesListingComponent },
            { path: 'plataformas', component: PlatformsListingComponent },
            { path: 'usuarios', component: UsersListingComponent },
            { path: 'jogos', component: GamesListingComponent },
            { path: '**', component: HomeComponent }
        ]
    },

    { path: 'cadastrar',
        children: [
            { path: '', component: HomeComponent },
            { path: 'fabricantes', component: ManufacturesRegisterComponent },
            { path: 'plataformas', component: PlatformsRegisterComponent },
            { …
Run Code Online (Sandbox Code Playgroud)

router routes angular2-routing

3
推荐指数
1
解决办法
7516
查看次数

将类应用于Angular中的每个路由组件

有没有办法将类应用于Angular中的每个路由组件.一种方法是使用host每个组件的属性

@Component({
    moduleId: module.id,
    selector: 'one',
    host :{class :'my-class'}
    templateUrl: 'one.html',
})
Run Code Online (Sandbox Code Playgroud)

但我不想为每个组件写这个.

typescript angular2-routing angular-components angular

3
推荐指数
1
解决办法
1151
查看次数

Angular 2,如何在ngOnInit()变量中存储值?

在这里,当我登录到我的应用程序时,我希望将json对象存储在本地存储中,然后将其分配给用户Object.第一次,如果我登录它将不会显示任何内容,但如果我刷新页面,我可以看到名字.

这是.ts文件

import { Component, OnInit } from '@angular/core';
@Component({
    selector: 'app-navbar',
    templateUrl: './navbar.component.html',
    styleUrls: ['./navbar.component.css']
})
export class NavbarComponent implements OnInit {
    user: Object;
    ngOnInit() {
    this.user = JSON.parse(localStorage.getItem('user'));
    }
}
Run Code Online (Sandbox Code Playgroud)

在模板中我想像这样使用它:

<p> {{ user.first_name }} </p>
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

javascript typescript angular2-routing angular

3
推荐指数
1
解决办法
3314
查看次数

在Angular 5 Ag-grid数据表中使用cellRendererFramework

我正在用angular 5构建一个应用程序。在我的HTML页面中,我有一个表,该表显示被查询的数据。此数据被显示在AG-网格使用指示。网格中的一列显示为HTML链接。我正在使用cellRendererFramework功能将列中的值显示为链接。它工作正常,并在表格中为每一行显示该列的值上的链接。我的要求是,我想将其他参数从主组件类传递给cellRendererFramework组件。我需要这个的原因是,当单击链接时,Angular应用程序使用angular路由器显示新组件,并且我需要将多个值传递给其他组件。

我不确定如何将参数传递给cellRendererFramework类。

数据网格的列定义

this.columnDefs = [
      { headerName: "Hotel ID", field: "HotelID", width: 500,
        cellRendererFramework: LinkcompComponent },
      { headerName: "Account Number", field: "AccountNumber" , width: 700 },
      { headerName: "Customer Name", field: "PartyName", width: 670  }
    ];
Run Code Online (Sandbox Code Playgroud)

cellRendererFramework组件的HTML文件

<a [routerLink]="['/trxDetails',params.value]">{{ params.value }}</a>
Run Code Online (Sandbox Code Playgroud)

是否可以将其他参数传递给cellRendererFramework组件?

ag-grid angular2-directives angular2-routing

3
推荐指数
1
解决办法
4623
查看次数

Angular2 +路由-可选的路由参数

所以我有这样的路线/category/tech,并/category/tech/new/category/tech/old

他们都用 ItemsComponent

因此,有什么方法可以像在ExpressJS中那样用可选的参数定义这些类型的路由。

router.get('/category/tech/:filter?', (req, res) => ...
Run Code Online (Sandbox Code Playgroud)

(在这里/category/tech/category/tech/xxx都会工作)

还是我必须像这样分别定义它们

{path: 'users', component: ItemsComponent},
{path: 'users/:filter', component: ItemsComponent}
Run Code Online (Sandbox Code Playgroud)

angular-ui-router angular2-routing angular

3
推荐指数
2
解决办法
3026
查看次数