How to dynamically compose a route in angular?

Asked
Active3 hr before
Viewed126 times

5 Answers

composedynamicallyangularroute
90%

Open the AppRoutingModule and add a new compose route to the appRoutes,,Open the AppRoutingModule and add a new admin route to its appRoutes array

Example_snippet/controller/utility/_compose.js/ content_copy . . .
      content_copy

      ng new angular - router - sample
load more v
88%

It's time to write the chunks of code for navigating between the component using routes,,In the routes file, we have created two main routes, /first and /second, and a default route that will redirect to /first

Example_snippet/controller/utility/_compose.js/ 1ng g c first --skipTests=true. . .
1n g g c first--skipTests = true
2n g g c second--skipTests = true
3n g g c first - child--skipTests = true
load more v
72%

In this article, I am going to show you an excellent Angular Router feature related to dynamic route population, It can be handy both for projects and unit testing

Example_snippet/controller/utility/_compose.js/ ng new medium-dynamic-routes -. . .
ng new medium - dynamic - routes--routing
cd medium - dynamic - routes
load more v
65%

This is required for consistency, Since the way we register routes in Angular 2 is by using @RouteConfig, we need to make sure that it’s always up-to-date, with all the routes available

Example_snippet/controller/utility/_compose.js/ @RouteConfig([ { path: '/', . . .
@RouteConfig([{
      path: '/',
      component: Home,
      name: 'home'
   },
   new AsyncRoute({
      path: '/about',
      loader: () => System.import('./components/about/about').then(m => m.About),
      name: 'about'
   })
])
load more v
75%

Of coures "MyComponent" is retrieved as a string, Now I want translate this array into Routes for a routing-module

Example_snippet/controller/utility/_compose.js/ export const routes: Route[] =. . .
export const routes: Route[] = [

];
@NgModule({
   declarations: [
      AppComponent,
      ErrorHandleComponent,
      FilterComponent,
      CreateComponent
   ],
   imports: [
      BrowserModule,
      RouterModule.forRoot(routes),
      // AppRoutingModule,
      HttpClientModule
   ],
   entryComponents: [
      CreateComponent,
      FilterComponent,
      ErrorHandleComponent
   ],
   providers: [],
   bootstrap: [AppComponent]
})
export class AppModule {}
Step 2 continued with import { CreateComponent } fro. . .
import {
   CreateComponent
} from '../create/create.component';
import {
   FilterComponent
} from '../filter/filter.component';
import {
   ErrorHandleComponent
} from '../error-handle/error-handle.component';

export const components = {
   'CreateComponent': CreateComponent,
   'FilterComponent': FilterComponent,
   'ErrorHandleComponent': ErrorHandleComponent,
};
Step 3 continued with constructor(private api: Api. . .
  constructor(private api: ApiService, private router: Router) {

  }
  getRoute() {
        this.api.get_route().subscribe(res => {
           res.forEach(element => {
              // components key value pair obj
              element.component = components[element.component];
              routes.push(element);
           });
           this.rlist = routes;
           this.router.resetConfig(routes);
        });
load more v