How to enable routing in Angular as Basic interview question ?

Anil Kumar
May 30, 2023
Photo by Denys Nevozhai on Unsplash

We have two case either routing may exist and added already at time of setup we just need to add our new path in routing file inside routes or we need to enable by some changes.

Step 1 add base ref to index.html

<head>
...
<base href="/" />
...
</head>

Step 2 main component should have router-outlet directive a placeholder for routed component.

<router-outlet> </router-outlet>

Step 2 then there should be a routing defined in main module or in routing module same should be imported in main module

 RouterModule.forRoot(
[
{
path: 'admin',
component: AdminComponent
},
......
]
)

like above example your path should have a component

in advance level if lazy loading or pre fetch logic need to be handled

--

--