日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Angular2学习笔记——在子组件中拿到路由参数

發布時間:2023/12/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Angular2学习笔记——在子组件中拿到路由参数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  工作中碰到的問題,特此記錄一下。

  Angular2中允許我們以`path\:id\childPath`的形式來定義路由,比如:

export const appRoutes: RouterConfig = [{path: 'app/:id', component: AppComponent,children: [{ path: 'share', component: AppShareComponent },{ path: 'issue', component: AppIssueComponent },{ path: 'version', component: AppVersionComponent },{ path: 'usage', component: AppUsageComponent },{ path: 'notification', component: AppNotificationComponent },{ path: 'resource', component: AppResourceComponent },{ path: 'comment', component: AppCommentComponent },{ path: 'activity', component: AppActivityComponent },{ path: 'retire', component: AppRetireComponent },{ path: '', component: AppComponent }] }];

  如果是在AppComponent中,很容易使用`ActivatedRoute`拿到當前路由獲取參數:

ngOnInit() {this.route.params.subscribe((params) => {this.createPies();this.onTopListFilterChange(params['id']);});};

  但如果是在`children`中指定的component要拿到路由參數就沒那么容易了,這時候再使用ActivatedRoute根本拿不到參數,我猜應當是在Angular2中一個ActivatedRoute對應一級路由配置,所以我們需要找到父級路由,由父級路由去拿參數。這時我們需要借用Router類的routeState屬性的parent方法:

this.router.routeState.parent(this.activatedRoute).params.subscribe(params => {this.getDetailsById(params['id']); })

  至此問題解決!

  Angular2給我的感覺是大框架很清晰,細節太瑣碎,使用后端開發思維來做前端,過于冗余。目前對Angular2了解并不深入,無法給出詳細解釋,待我深入了解后,再寫一篇關于路由的文章奉獻給大家。

轉載于:https://www.cnblogs.com/dojo-lzz/p/5883408.html

總結

以上是生活随笔為你收集整理的Angular2学习笔记——在子组件中拿到路由参数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。