簡體   English   中英

無法在應用組件的 angular 6 中找到路由自定義數據

[英]Not able to find route custom data in angular 6 in app component

我有一個 angular 應用程序,並將自定義數據添加到給出的路線中

{ path: 'profile', component: ProfileComponent, data: {title: 'example'} }

在 app.component.ts 文件中,我有以下代碼

private route: ActivatedRoute

console.log('route snapshot', this.route.firstChild.data._value.pageType );

我得到的錯誤是

ERROR in src/app/app.component.ts(71,64): error TS2339: Property '_value' does not exist on type 'Observable<Data>'.

誰能告訴我如何訪問 app.component.ts 中的路由數據

更新

您將需要訂閱 AppComponent 中的路由器事件並從中獲取參數:

constructor(private router: Router, route: ActivatedRoute) { }

ngOnInit() {
  this.router.events.subscribe(event => {
    if(event instanceof NavigationEnd){
      console.log(this.route.root.firstChild.snapshot.data['title']);
    }
  });
}

如果您只希望它用於該路徑,則可以再添加一個條件

if(event instanceof NavigationEnd && event.url === '/profile')

舊答案

您將需要訪問快照以獲取數據參數

this.route.snapshot.data['title'];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM