Link to home
Start Free TrialLog in
Avatar of error2013
error2013

asked on

Angular 5 - Module read routes from external json file

I have created a new project using Angular-Cli with the --routing option.

So my routing module looks like this:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import {MyComponent} from './components/my/my.component';

const routes: Routes = [
  {path: '', component: MyComponent}
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

Open in new window


What I need to do is to add the route data to a json file and have the module load the data when the module loads.

So when the app start is would read data from a json file ...and create the routes:

For example:
const routes: Routes = [
  {path: '', component: MyComponent}
];

Open in new window


How can I do this?
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of error2013
error2013

ASKER

I've tried something similar before by just creating a module with the data and using export and then import.

But as for router.resetConfig, I've never been able to make it work so it was either me of it just has a bug.

Have you been able to get it to work before?
As I mentioned in my earlier post I have not personally implemented this but have researched it. There is not a lot on it - the information I sent you is currently all that I was able to find.

If I have time I will try an implementation and post back here.

The question to ask is - why do you need a dynamic config?

Can you not achieve the same thing with a global config and then turning off items with a role based security solution?
I'm trying to get components to load by external configuration done at runtime as this was just part of the puzzle.
Again the question I have - what is the reason for loading them externally as opposed to having them in the app and only enabling them when you need them?
The answer to that question is that, I'm trying to get unknown(At the time of compile) components for load by modifying the routes data.
You have another question open about how to dynamically load components - how do you want to handle this question going forward - what do you need on this question that is not overlapping with the other?
Well, like I said...this question is one part of the puzzle and the other question is the whole target. The reason why I've broken it into parts that I want to know is that I have found no examples on the internet on how to do the (other question) and no-one seems to know, so I just don't know how to handle it :)
If you google Dynamically add components and ComponentFactoryResolver you should find examples. The code in those samples is too extensive to post here.