0

我是新的角度2开发。我添加了一个组件,在这个组件中,我有一些其他的子组件。在以下方面,我的文件夹结构:子页面中的角度2路由问题

parentComponent 
    -child1Component 
    -child2Component 
    -child3Component 

parentComponent.html我加入<router-outlet></router-outlet>

我从child1到孩子2导航,和儿童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 
+0

请你提供一个工作的例子吗? –

回答

0

它与下面。

在Index.html页面和ParentComponent的html页面添加路由器插座。

--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" /> 
+0

我在我的代码中使用相同的结构只有一个区别是我从我的组件路由而不是[routerLink] – user3541485

+0

您可以发布代码吗? – RRForUI

+0

用组件导航编辑我的答案 – RRForUI