Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to build add routes dynamically in angular 2? [duplicate]

Tags:

angular

I am building a cms where a user can pick what pages to use. So if the user wants a contact page, they select it and fill out some content for it and their site would have a contact route for example. Based on this, is it possible to dynamically add routes in angular based on if a user wants or does not want a particular page?

like image 680
xaisoft Avatar asked Mar 21 '17 13:03

xaisoft


People also ask

What would you use in angular 2 to define routes?

Instead of “href” attribute of anchor tag, we use the “routerLink” attribute of Angular. The routerLink attribute allows us to link to a specific route of the Application.

What is route reuse strategy in angular?

BaseRouteReuseStrategylink This base route reuse strategy only reuses routes when the matched router configs are identical. This prevents components from being destroyed and recreated when just the fragment or query parameters change (that is, the existing component is reused).

Does angular add routing?

At the basic level, routing allows angular to display different "pages" or components. You probably want to have it, if you want to navigate across pages in your application. It shouldn't hurt anything if you add it, but don't use it.

What data is required for dynamic routing in angular?

There is one additional piece of data that Angular requires for dynamic routing. Any Route that is provided dynamically must be defined in your Appplication’s NgModule as a set of ‘entryComponents.’

Why is the injector used for dynamic routing in angular?

The Injector is used since you would otherwise get a circular reference by attempting to inject the Router. There is one additional piece of data that Angular requires for dynamic routing. Any Route that is provided dynamically must be defined in your Appplication’s NgModule as a set of ‘entryComponents.’

How do I know if the default route works in angular?

If you run the application now with the ng serve --open command, you should see the “home works!” label on the main page. That means the default route works as expected, and shows us the auto-generated by Angular CLI content.

How to dynamically add new routes to an existing route?

By modifying ActivatedRoute.routeConfig directly, it is also possible to dynamically add new routes. This is very handy when you want to add new routes as children of a route existing deeply in route hierachy. And maybe it is the only way when the route is in a lazily loaded module. The bad news is that it lacks validation on newly added routes.


1 Answers

Yes. There are multiple ways to accomplish what you are wanting. You can use router.resetConfig to reset the router's config with a new config:

this.router.resetConfig([  { path: 'somePath', component: SomeComponent},  ... ]); 

You can also push routes onto the config:

this.router.config.push({ path: 'somePath', component: SomeComponent }); 

Another way I've done this is I had made a way users could build pages using something like TinyMCE and specify the url path they wanted the page to have. In Angular, I specified a wild-card route in the router with { path: '**', component: ComponentBuilder }, where the ComponentBuilder component would fetch the template from the database that corresponds to the path someone requested and then dynamically generate the component with the template. If a template didn't exist in the database, then a normal Page cannot be found template was rendered.

Both ways work. It comes down to how you want your application to function and how you will build the pages users create.

There is a lot more documentation and examples on how to create dynamic components since the solution I came up with in version 2 rc4. I have not used any of these, but it looks helpful:

NPM package for Dynamic Components

Angular Docs on Dynamic Component Loader

Dynamically Creating Components

Another article on dynamically creating components

like image 170
Tyler Jennings Avatar answered Sep 22 '22 06:09

Tyler Jennings