Variables within Angular routes being available in components

145 Views Asked by At

Weather (pun intended) it is the heat of the day, or the day of the week (Friday) but for the life of me, I cannot fathom out how to pass data from within a defined route to a component.

In my app.routing.ts file, I have the following route:

path: '',
component: AppBlankComponent,
children: [{
    path: 'reports',
    loadChildren: './reports/reports.module#ReportsModule',
    data: { timeout: false }
}]

I am after being able to assign a variable from the data section containing the element 'refresh'. This will enable me to switch on and off the ability for individual routes to have an idle time out from being applied to them. (The timeout basically throws up a dialogue box warning the user that they have been inactive and will be logged out if they don't do something within the application.)

For the login page (which a user hasn't logged in yet), I simply use an IF statement within the app.component.ts, but with more pages being added outside of the authentication/logging on mechanism, the IF statement will become very unwieldy very quickly. Therefore I thought if I could add a data element, I can easily add more routes without amending the code itself.

I've tried a variety of ways from Googling the internet, but none has actually worked as most seem to be concentrating on the parameters in the URL as opposed to the actual route object.

One example:

public ngOnInit(): void {

    console.log(this.activatedRoute.snapshot.data);

    this.activatedRoute.queryParams.subscribe(params => {
        console.log(params["timeout"]);
    });
}

What I would like to do if something akin to:

        if (isset(this.xyz.data) && this.xyz.data['timeout'] === true)
        {
            this.idle.stop();
            this.keepalive.stop();
        }    
        else
        {
            this.idle.watch();
            this.keepalive.start();
        }

This is currently on Angular 6.

Any help would be gratefully received.

0

There are 0 best solutions below