Angular ActivatedRoute data returns an empty object

后端 未结 5 1422
时光说笑
时光说笑 2020-12-15 02:43

I have a route registered with some data:

const routes: Routes = 
[
    {path: \'my-route\', data: { title: \'MyTitl         


        
相关标签:
5条回答
  • 2020-12-15 03:02

    I Don't know the version spoken, but as of Angular 6, this worked for me:

    (Ofcourse thanks to shinDath)

      routeData;
      ngOnInit() {
        //child route param doesnt go up to parent route params.
        this.router.events.subscribe((val) => {
          if (val instanceof ActivationEnd) {
            if(!$.isEmptyObject(val.snapshot.params)){
              this.routeData = val.snapshot.params;
            }
          }
        });
      }
    
    0 讨论(0)
  • 2020-12-15 03:10

    Below should work:

    constructor(private route: ActivatedRoute) {}
    
    ngOnInit() {
        console.log(this.route.snapshot.data);
    }
    
    0 讨论(0)
  • 2020-12-15 03:16

    Edit: the problem is that I was trying to access the ActivatedRoute from a Component which is outside the <router-outlet>. So it looks like that this is the intended behaviour.

    However I still think that my answer below can be useful to anyone who is trying to accomplish the same thing.


    I found a workaround on GitHub (thanks manklu) that I used in order to accomplish what I needed:

    import { Component, OnInit } from '@angular/core';
    import { Router, RoutesRecognized } from '@angular/router';
    
    @Component({...})
    export class MyComponent implements OnInit {
      private routeData;
    
      constructor(private router: Router) { }
    
      ngOnInit() {
        this.router.events.subscribe((data) => {
          if (data instanceof RoutesRecognized) {
            this.routeData = data.state.root.firstChild.data;
          }
        });
      }
    }
    

    doing this way this.routeData will hold the route data that I needed (in my case the page title).

    0 讨论(0)
  • 2020-12-15 03:19

    This is part of my code (NOTE: I'm using Angular 8):

    constructor(private _router: Router, private _route: ActivatedRoute) {}
    
    ngOnInit() {
      ...
      this.listenRouting();
    }
    
    listenRouting() {
        this._router.events.subscribe((router: any) => {
          routerUrl = router.url;
          if (routerUrl && typeof routerUrl === 'string') {
            routerList = routerUrl.slice(1).split('/');
            console.log("data.breadcrumbs", this._route.snapshot.routeConfig.children.find(child => child.path === routerList[routerList.length - 1]).data.breadcrumbs);
            ...
          }
        });
    }
    

    So data is "hiding" under ActivatedRoute.snapshot.routeConfig.children Array. Each child contains, among other things, data. In my case data is configured in routing.module, e.g.:

    const routes: Routes = [
      { path: "facility-datas",
        component: FacilityTerminal,
        data: {
          breadcrumbs: "b ft"
        }
      },
    ...
    ];
    
    0 讨论(0)
  • 2020-12-15 03:19
    import { AfterViewInit, Component} from '@angular/core';
    import { ActivatedRoute } from '@angular/router';
    
    
    @Component({
      selector: 'app-users',
      templateUrl: './users.component.html',
      styleUrls: ['./users.component.scss']
    })
    export class UsersComponent implements AfterViewInit {
      paramId: string;
    
      constructor(private activatedRoute: ActivatedRoute) {}
    
      ngAfterViewInit(): void {
        this.paramId = this.activatedRoute.snapshot.params.id;
        console.log('paramId =', this.paramId);
      }
    
    }
    
    0 讨论(0)
提交回复
热议问题