0

In the routing, when we pass some value as paramters from one component to another, the passed parameters values gets appended to the browser url... is there any way it can be avoided? enter image description here

In the example from snapshot attached "Administrator" is the username which I am passing from home.ts component

My routing is as follows

[![export const routes: RouterConfig = \[
    { path: '', component: Login },
    { path: 'login', component: Login },
    { path: 'Home/:userName', component: Home },
    ...HOME_ROUTER_PROVIDERS
    }
\]

export const HOME_ROUTER_PROVIDERS: RouterConfig = \[
    {
        path: 'Home/:userName',
        component: Home,
        children: \[

            { path: 'Dashboard1', component: Dashboard1 },
            { path: 'Dashboard2', component: Dashboard2 },
            ....
    }
\]][1]][1]

My home.ts where I do the navigate.route to pass the parameters

export class Home {
        userName: any;
        constructor(private activatedRoute: ActivatedRoute) {
            console.log("In constructor of Home");
            activatedRoute.params.subscribe(params => {
                this.userName = params['userName']
            });
        }
    }
1
  • The routers job is to reflect the state to the browsers URL bar and the URL to the state. If you don't wan't parts of it reflected in the URL bar, then don't maintain this state using the router. In addition to @MadhuRanjan s answer see angular.io/docs/ts/latest/cookbook/… Commented Aug 19, 2016 at 5:07

1 Answer 1

2

you may create a UserService using it in your Login component you may set some variable which decides if the logged in user is administrator or not.

And when your Home component is routed you may read role value from UserService and do stuff using the value.

Sign up to request clarification or add additional context in comments.

2 Comments

This will cause error when refreshing a browser. How can we avoid that situation?
A general answer to the title of the question migth be found here stackoverflow.com/questions/46377565/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.