As I was working on my project, I had an enumeration set up like this
export enum RootPage {
HOME = <any>'HomePage',
LOGIN = <any>'LoginPage',
TEST01 = <any>'Test01Page',
LAUNCHPAD = <any>'LaunchpadPage',
JOBS = <any>'JobsPage',
MACHINES =<any>'MachinesPage',
GVARS =<any>'GvarsPage',
RESOURCES=<any>'ResourcesPage',
CONFIGURATION=<any>'ConfigurationPage',
TABS=<any>'TabsPage'
};
Initially, I believed that I could simply do the following:
constructor(
private menuitemsService:MenuitemsService
) {}
let pageString = 'HomePage';
let rp:RootPage = RootPage[pageString];
let pi:PageInterface = this.menuitemsService.getPagebyRoot(rp)
When it came to the MenuItemsService:
getPagebyRoot(rootPage:RootPage): PageInterface {
...
}
However, the issue arose when 'MenuItemService.getPagebyRoot()' interpreted rp as HOME instead of HomePage.
Thus, I had to create a utility method in MenuItemService containing a cumbersome switch statement.
getRootPageFromString(name:string):RootPage {
switch (name) {
case RootPage.HOME.toString():
return RootPage.HOME;
case RootPage.LOGIN.toString():
return RootPage.LOGIN;
case RootPage.TEST01.toString():
return RootPage.TEST01;
case RootPage.LAUNCHPAD.toString():
return RootPage.LAUNCHPAD;
case RootPage.JOBS.toString():
return RootPage.JOBS;
case RootPage.MACHINES.toString():
return RootPage.MACHINES;
case RootPage.GVARS.toString():
return RootPage.GVARS;
case RootPage.RESOURCES.toString():
return RootPage.RESOURCES;
case RootPage.CONFIGURATION.toString():
return RootPage.CONFIGURATION;
case RootPage.TABS.toString():
return RootPage.TABS;
}
Eventually, I opted to replace the line in my code with:
let rp:RootPage = this.menuItemService.getRootPageFromString(pageString);
There must be a more straightforward solution.
It was frustrating that I had to resort to this workaround.
Have others encountered similar challenges?
Or discovered a more elegant resolution?
Update:
I have shared my own solution to the problem. The issue stemmed from using TypeScript 2.3.4 instead of 2.4+