Implementing Routes
In previous topic we have created our login page and added validation, in this topic we will cover routing in angular when user click on login button they will route to welcome page or error page.
If you remember when we create our project using "ng new" command there were some question asked i.e. do we need routing and we said Yes and then we got one file app-routing.module.ts so this is the module which will handle all the routing in our application. All the routes are configured under "const routes: Routes = [];"
First we need to create our welcome and error components for that we can use command "ng generate component welcome" and "ng generate component error".
After generating these components now we will configure routings.
in app-routing.module.ts need to add below code:
It means path will be http://localhost:4200/login and which component it will call, it will call loginComponent. you have import all component in app-routing.module.ts
Similarly for welcome component and error component we need to path and component in separate curly braces {} separated by comma.
http://localhost:4200/error
http://localhost:4200/welcome
http://localhost:4200/login
if you go to these URL you can see it is not opening respective welcome and error page it will always load login page which is default. so what is the issue here ?
This is because we haven't enabled routing tag for that go to app.component.html and add <router-outlet></router-outlet> and remove<app-login></app-login>because of this login page is loaded every time. Now refresh your page and check
each URL.
If you hit any other URL like http://localhost:4200/login12234 there will be
runtime error and you can check this in console of inspect window. Here you cannot show this error to your user, you need to show some meaningful message for this you need to add error message and error.component.ts and add it in error.component.html using interpolation.
For routing to work if any other URL comes which is not mentioned in routes it will route to error page we need to add it in app-routing.module.ts see below screenshot.
Now if you hit only http://localhost:4200 then you can see nothing is loaded so for this we need to add route so that it will land to login page
This will come for malicious URL.
Routing from Login Page to Welcome Page
Now Refresh your page and login with correct username and password you can see it is routed to welcome page.
Now lets add username in the welcome page, one of the way is by adding the parameter in URL so that router can access it. so we have to enhance our app-routing.module.ts to accept username as parameter.
now URL is expecting username and this will be routed to welcome component so we have to enhance our welcome component. For that we need to add dependency called Activated Route in our welcome.commponent.ts, after adding the dependency in constructor now we can update our noOnInit() method.
There is one method snapshot which will give what all parameter do we have in the URL and we can use this to get username from URL.
Now if you click on login button you will see error because it is routed to /welcome page but we have updated our code to /welcome/:username so we need to update our login.component.ts, we have to update below code in handleLogin() method. we have to pass username as parameter.
Previous Topic (Login Component) Next Topic (Create todo list)
Comments
Post a Comment