0

I have to create routing file in Angular Project, and all path and component name are stored in database.

component will select as per login user rights

now I want to crate route array in routing module file.

I will get all rout path from data base according to user and make route array.

But I have no idea how to convert string component name to component.

Please help if anyone have idea.

1
  • Provide some code Commented Aug 17, 2019 at 10:28

1 Answer 1

1

You can write a function to return each component type based on the string value, something like this:

convertComponent(componentName: string) {
    switch(componentName) {
        case 'SomeComponent':
            return SomeComponent
            break
        case 'SomethingelseComponent':
            return SomethingelseComponent
            break
        // etc
    }
}

Then call it when you are making your routing module:

{ path: 'something/', component: convertComponent(componentNameString) },

You can also use a registry map instead of the switch case, and set the elements in each component you define.

Sign up to request clarification or add additional context in comments.

4 Comments

You are right but still I have define all component in switch case. I have array which contains path and component name and on the bases of that I want to crate route array for app.routing.ts file. so I will give permission to selected user for only selected components. and on that route file user get their working menues
What's the problem of defining all components in the switch case?
that is already I did. But I have more than 100 components and it will be increase more. each time I have to put in switch case and make production and put on server. I will insert path and component name in table and when project run it will make it's route array in app.routing.ts. so when ever I want to delete or insert component in table in database as per user login it will show. So I think typecast will usefull to me but I have no idea how to use.
I don't see any problem to edit the switch case, while you are writing so much code for the component itself. As I think, the solutions to this problem are switch case OR using registry maps. Anyway, you can take a look at this answer if you don't wanna use those 2 methods and wanna go in a dirty way, and ready to handle unknown exceptions in your code: stackoverflow.com/a/55178914/1185148

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.