Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular, get parent route component

I am building an Angular 7 app. In this app I got nested routes. I want to be able to detect what component the parent route is using. I found a way of doing it locally but this does not work on production (output is different).

I use this method:

   checkIfChild() {
    this.sub = this.route.parent.params.subscribe(params => {
      if (params['id']) {
        this.parentId = params['id'];
        if (this.route.parent.component['name'] === 'ProjectShowComponent') {
          this.parentType = 'Project';
        } else if (this.route.parent.component['name'] === 'CompanyShowComponent') {
          this.parentType = 'Company';
        } else if (this.route.parent.component['name'] === 'ContactShowComponent') {
          this.parentType = 'User';
        }
      }
    });
  }

The method, this.route.parent.component['name'], outputs the name locally but just the letter T on production.

I get this message instead

TypeError: 'arguments', 'callee', and 'caller' cannot be accessed in this context.

What is the right way of detecting what parent route has activated the child route so that I can act on it?

like image 719
Jonathan Clark Avatar asked Feb 02 '19 11:02

Jonathan Clark


People also ask

How do you navigate to a parent route from a child route?

The routerLink is set to the parent component route. Just to add up, queryParams directive is used to send a message to the parent component via the query string. Within the message variable the parameter is received and the message s stored. It captures using the ActivatedRoute class.

How do you access the parent component property in a child component?

In the parent component, declare the property that you want to receive in the child component, say 'ParentId'. While including the child component inside the parent component, bind the 'ParentId' property to the child component using property binding.

What is Parammap in angular?

ParamMaplinkA map that provides access to the required and optional parameters specific to a route. The map supports retrieving a single value with get() or multiple values with getAll() .

What is skipLocationChange?

skipLocationChange: boolean. You can change the route, without changing the URL in the browser. This Navigates to a new URL without pushing a new state into history.

How to route to parent from child in angular?

The routing should be defined within the app-routing.module.ts file as follows: Once it is done, anyone of the two methods can be used to route to parent from a child. To register routes in angular for any component, set the path and the class name of the component inside the app-routing.module.ts file.

How does activatedroute work in angular?

In Angular, each View component is associated with an ActivatedRoute instance. But, unlike many other services in Angular, ActivatedRoute is not a singleton. Instead, each View component is injected with its own unique instance of ActivatedRoute. This ActivatedRoute instance gives the View component access to the state of the local route segment.

What is a child component in angular?

In angular, a root component that serves as the parent component of all the components and rest of the other components can be called a Child Component to the root component.

What is the parent property of an activatedroute?

The "parent" property gives // you direct access to the ActivatedRoute instance associated with the parent // segment of the current route.


1 Answers

Personally, I would drop the direct coupling to the component instance and instead use the data property of the route, considering that:

  • You arent interacting with the component instance in any way.
  • You map the component instance type to a static value.

Assuming the following routes definition:

const routes: Routes = [
  {
    path: 'production',
    component: ProductionParent,
    data: {parentRoute :'Production'},
    children: [{path: '', component: Child}] 
  },
  {
    path: 'system',
    component: SystemParent,
    data: {parentRoute :'System'},
    children: [{path: '', component: Child}] 
  }
];

@Component({})
export class ProductionParent{}

@Component({})
export class SystemParent{}

@Component({})
export class Child implements OnInit, OnDestroy {
  private parentSub = Subscription.EMPTY;
  parentRoute :string;


  constructor(private readonly route: ActivatedRoute){}

  ngOnInit(){
    this.trackParent();
  }

  ngOnDestroy(){
   this.parentSub.unsubscribe();
  }

  private trackParent(){
    this.parentSub = this.route.parent
                        .data
                        .subscribe(data => this.parentRoute = data.parentRoute || 'unknown');
  }
}

This can most likely be implemented in other ways, but this is the first pragmatic approach that came to my mind. Hope it helps.

like image 118
Jota.Toledo Avatar answered Oct 19 '22 23:10

Jota.Toledo