使用Angular2/4中的相对路径以编程方式路由到其他组件
这是我的文件夹结构:使用Angular2/4中的相对路径以编程方式路由到其他组件,angular,routing,Angular,Routing,这是我的文件夹结构: App -KitchenComponent -css -html -kitchen.component.ts -BedroomComponent -css -html -kitchen.component.ts app.component.html app.component.ts 我想使用相对路径以编程方式从厨房组件移动到卧室组件 KitchenComponent.html <
App
-KitchenComponent
-css
-html
-kitchen.component.ts
-BedroomComponent
-css
-html
-kitchen.component.ts
app.component.html
app.component.ts
我想使用相对路径以编程方式从厨房组件移动到卧室组件
KitchenComponent.html
<button (click)="moveToBedroomComponent()"></button>
KithcenComponent.ts
Constructor(private router: Router){}
moveToBedroomComponent(){
this.router.navigate(['bedroom'], {relativeTo:_____});
}
我想我无法在这里给出相对路径,请注意输入。relative to参数引用激活的路径:
@Component({...})
class KitchenCmp {
constructor(private route: ActivatedRoute, private router: Router) {
}
moveToBedroomComponent(){
this.router.navigate('../bedroom', {relativeTo:this.route});
}
}
relativeTo参数引用激活的路由:
@Component({...})
class KitchenCmp {
constructor(private route: ActivatedRoute, private router: Router) {
}
moveToBedroomComponent(){
this.router.navigate('../bedroom', {relativeTo:this.route});
}
}
首先,最好能看到你的app.route.ts文件 我是这样做的: 这个。_路由器。导航['dashboard'] My app.route.ts包含{path:'dashboard',组件:HomeComponent,canActivate:[AuthGuard]} 还要确保您的导入是正确的: 从'@angular/Router'导入{Router,ActivatedRoute} 编辑
你确定你的路由器需要相对位吗?乍一看,你似乎不知道。首先,最好看看你的app.route.ts文件 我是这样做的: 这个。_路由器。导航['dashboard'] My app.route.ts包含{path:'dashboard',组件:HomeComponent,canActivate:[AuthGuard]} 还要确保您的导入是正确的: 从'@angular/Router'导入{Router,ActivatedRoute} 编辑
你确定你的路由器需要相对位吗?乍一看,你似乎不知道。嘿,有什么不清楚的地方吗?嘿,有什么不清楚的地方吗?Max,当你插入“../”时,它变成了绝对路径,对吗?@MichaelPhilips,不,它是当前路线的相对路径Max,当你插入“../”时,它变成了绝对路径,对吗?@MichaelPhilips,不,它是当前路线的相对路径