Angular2

时间:2016-05-05 16:55:13

标签: angular angular-ui-router ngroute

我正在尝试创建一个具有2个(可能是3个)导航级别的应用程序。我正在使用@RouterConfig创建此导航。

当我创建第一级导航时,应用程序工作正常,所以我通过相同的过程来创建导航的第2级。

我想要创建的导航格式如下

mainApplication (sidebar navigation)

---select (tabs)
   ---by Collection
   ---by Category
   ---by Range

---Inspect (tabs)
   ---forecast
   ---inventory
   ---purchase

---Other
   ---sublevel
   ---sublevel
   ---sublevel
---Other
   ---sublevel
   ---sublevel
---Other

我收到以下错误,我不明白......

" / inspect"不允许使用子路由。使用" ..."在父母的路线上。

在这个例子中,我将重点关注Inspec组件中的子导航。以下是代码......

app.component.ts

import { Component,OnInit}  from 'angular2/core';
import { Routes, Router, ROUTER_DIRECTIVES ,ROUTE_PROVIDERS,RouteConfig} from 'angular2/router';


//ROUTER PAGES
import {ProjectSelect}          from 'app/project.select'
import {ProjectInspect}         from 'app/project.inspect'


@Component({
    selector        :  'my-app',
    templateUrl :  'app/app.component.html',
    styleUrls   : ['app/app.component.css']
    directives  : [
                                        ROUTER_DIRECTIVES,
                                        ProjectSelect,
                                        ProjectInspect
        ]
})

@RouteConfig([
    {path: '/select',   name:'Select',  component: ProjectSelect, useAsDefault: true},
    {path: '/inspect',  name:'Inspect', component: ProjectInspect},
])


export class AppComponent implements OnInit {

    applicationTitle:String = "Slim 4 Angular"

  constructor(private router: Router) {}

  ngOnInit() {
    this.router.navigate(['Select']);
  }
}

app.component.html

<div class="mdl-layout mdl-js-layout mdl-layout--fixed-drawer">
    <div class="mdl-layout__drawer">
        <span class="mdl-layout-title">{{applicationTitle}}</span>
        <nav class="mdl-navigation">
            <a class="mdl-navigation__link" [routerLink]="['Select']">Select</a>
            <a class="mdl-navigation__link" [routerLink]="['Inspect']">Inspect</a>
            <a class="mdl-navigation__link">Order</a>
            <a class="mdl-navigation__link">Intelligence</a>
            <a class="mdl-navigation__link">Demand</a>
            <a class="mdl-navigation__link">Introduce</a>
            <a class="mdl-navigation__link">Update</a>
            <a class="mdl-navigation__link">Promote</a>
        </nav>
    </div>
    <main class="mdl-layout__content">
        <div class="page-content">
            <router-outlet></router-outlet>
        </div>
    </main>
</div>

project.inspect.ts

import { Component,OnInit}  from 'angular2/core';
import { Routes, Router, ROUTER_DIRECTIVES ,ROUTE_PROVIDERS,RouteConfig} from 'angular2/router';


//ROUTER PAGES
import {WorkbenchForecast}      from 'app/workbench.forecast'
import {WorkbenchInventory}     from 'app/workbench.inventory'
import {WorkbenchPurchase}      from 'app/workbench.purchase'



@Component({
    selector        :  'project-inspect',
    templateUrl     :  'app/project.inspect.html',
    styleUrls   : ['app/app.component.css']
    directives  : [
                                        ROUTER_DIRECTIVES,
                                        WorkbenchForecast,
                                        WorkbenchInventory,
                                        WorkbenchPurchase
        ]
})


@RouteConfig([
    {path: './forecast',    name:'Forecast',    component: WorkbenchForecast, useAsDefault: true},
    {path: './inventory', name:'Inventory', component: WorkbenchInventory},
    {path: './purchase',    name:'Purchase',    component: WorkbenchPurchase},
])


export class ProjectInspect implements OnInit {

    projectTitle:String = "Inspect"

  constructor(private router: Router) {}

  ngOnInit() {
    this.router.navigate(['Forecast']);
  }
}

project.inspect.html

<div class="mdl-tabs mdl-js-tabs mdl-js-ripple-effect">
  <div class="mdl-tabs__tab-bar">
      <a href="inspect/#workbenck1" class="mdl-tabs__tab" [routerLink]="['Forecast']">Forecast</a>
      <a href="inspect/#workbench2" class="mdl-tabs__tab" [routerLink]="['Inventory']">Inventory</a>
      <a href="inspect/#workbench3" class="mdl-tabs__tab" [routerLink]="['Purchase']">Purchase</a>
    </div>
    <div class="page-content">
            <router-outlet></router-outlet>
    </div>
</div>

我不确定在Angular2中是否可以实现两级导航,但我希望可以。如果有人能帮助我正确实施,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

app.component.ts中进行以下更改:

@RouteConfig([
    {path: '/select/...',   name:'Select',  component: ProjectSelect, useAsDefault: true},
    {path: '/inspect/...',  name:'Inspect', component: ProjectInspect},
])