子页面中的Angular 2路由问题

时间:2017-08-03 14:40:10

标签: angular angular-routing angular-router angular-route-guards

我是新的角度2开发。我添加了一个组件,在这个组件里面我有一些其他的子组件。我的文件夹结构有以下几种:

parentComponent
    -child1Component
    -child2Component
    -child3Component

parentComponent.html我添加了<router-outlet></router-outlet>

我有从child1到child 2的导航,以及child 2到child3的导航。我也可以向后移动,反之亦然。回去我正在使用this._location.back();

当我使用后退按钮向后移动时。它显示当前路线中的上一页组件。

例如:

当我使用后退按钮从child2移动到child1时。 child1路由仍在child1页面中显示子html部分。

对于我正在使用的路由:

{ path: "parent", component: parentComponent,
  children: [ 
    { path: "child1", component: child1Component },
    { path: "child2", component: child2Component },
    { path: "child3", component: child3Component}
  ],
     data: { breadcrumb: 'Parent' }
}

我不知道为什么会这样。请帮助!!

版本信息

@angular/cli: 1.2.6
node: 6.10.0
os: win32 x64
@angular/animations: 4.3.2
@angular/common: 4.3.2
@angular/compiler: 4.3.2
@angular/core: 4.3.2
@angular/forms: 4.3.2
@angular/http: 4.3.2
@angular/platform-browser: 4.3.2
@angular/platform-browser-dynamic: 4.3.2
@angular/router: 4.3.2
@angular/cli: 1.2.6
@angular/compiler-cli: 4.3.2
@angular/language-service: 4.3.2

1 个答案:

答案 0 :(得分:0)

适用于以下。

在Index.html页面和ParentComponent的html页面中添加router-outlet。

- routes.ts

 {
    path:'',
    redirectTo:'index.html',
    pathMatch:'full'
},
{
    path:'parent',
    component:ParentComponent,
    children:[
        {
            path:"child1",
            component:Child1Component
        },
        {
            path:"child2",
            component:Child2Component
        },
        {
            path:"child3",
            component:Child3Component
        }
    ]

}

- Parent.component.html

 <input type="button" (click)="goNext()" value="Next">  <router-outlet></router-outlet>

- parent.component.ts

  goNext()
  {
    this.router.navigateByUrl("/parent/child1")
  }

child1.html

  <input type="button" (click)="goNext()" value="Next"><input type="button" (click)="goBack()" value="Back" />

chil1.ts

    goNext()
  {
    this.router.navigateByUrl("/parent/child2")
  }

child2.html

 <input type="button" (click)="goNext()" value="Next"><input type="button" (click)="goBack()" value="Back" />

child2.ts

    goNext()
  {
    this.router.navigateByUrl("/parent/child3")
  }

child3.html

 <input type="button" (click)="goBack()" value="Back" />
相关问题