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?
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.
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.
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() .
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.
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.
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.
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.
The "parent" property gives // you direct access to the ActivatedRoute instance associated with the parent // segment of the current route.
Personally, I would drop the direct coupling to the component instance and instead use the data
property of the route, considering that:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With