[英]Routing programmatically to other component using relative path in Angular2/4
這是我的文件夾結構:
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:_____});
}
我認為我在這里只能提供相對路徑,感謝大家的投入。
首先,很高興看到您的app.route.ts
文件。
我這樣導航: this._router.navigate(['dashboard']);
我的app.route.ts
包含{path: 'dashboard', component: HomeComponent, canActivate: [AuthGuard]}
還要確保導入正確: import { Router, ActivatedRoute } from '@angular/router';
您確定需要router
relativeTo
位嗎? 乍一看,您似乎沒有。
relativeTo
參數采用對已激活路線的引用:
@Component({...})
class KitchenCmp {
constructor(private route: ActivatedRoute, private router: Router) {
}
moveToBedroomComponent(){
this.router.navigate('../bedroom', {relativeTo:this.route});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.