I’m building an app where the data comes from the server. The menu is dynamic, and the server can send different menus depending on the user profile. I will never know what will be the component/page, that the menu item will have, I just need to have that component/page available.
The main problem is that I need to do a lot of components/pages imports and I do not want to that in every component/page.
I created a service, where all the imports are made and a method exists to return the correct component, so the navController could navigate.
import { Injectable } from '@angular/core';
//Import navigation pages
import { NotificationsPage } from "./../pages/notifications/notifications";
import { ClubPage } from "./../pages/club/club";
import { ActivitiesPage } from "./../pages/activities/activities";
import { NewsPage } from "./../pages/news/news";
import { CalendarPage } from "./../pages/calendar/calendar";
import { NewsletterPage } from "./../pages/newsletter/newsletter";
import { SettingsPage } from "./../pages/settings/settings";
import { FavoritesPage } from "./../pages/favorites/favorites";
import { SubmenuPage } from "./../pages/submenu/submenu";
import { ContentDetailsPage } from "./../pages/contentdetails/contentdetails";
@Injectable()
export class ViewNavigatorService
{
public NOTIFICATIONS:string = "notifications";
public CLUB:string = "club";
public ACTIVITIES:string = "activities";
public NEWS:string = "news";
public CALENDAR:string = "calendar";
public NEWSLETTER:string = "newsletter";
public SETTINGS:string = "settings";
public FAVORITS:string = "favorits";
public SUBMENU:string = "submenu";
public CONTENT_DETAILS:string = "content-details";
public GetComponent(page:string, data?:any):any
{
let Component = null;
switch (page)
{
case this.NOTIFICATIONS: Component = NotificationsPage; break;
case this.CLUB: Component = ClubPage; break;
case this.ACTIVITIES: Component = ActivitiesPage; break;
case this.NEWS: Component = NewsPage; break;
case this.CALENDAR: Component = CalendarPage; break;
case this.NEWSLETTER: Component = NewsletterPage; break;
case this.SETTINGS: Component = SettingsPage; break;
case this.FAVORITS: Component = FavoritesPage; break;
case this.SUBMENU: Component = SubmenuPage; break;
case this.CONTENT_DETAILS: Component = ContentDetailsPage; break;
default: Component = null; break;
}
return Component;
}
}
If I only inject the service in a parent page it works, but if I also inject the service in a child page, I get this error:
Can’t resolve all parameters for SubmenuPage: (NavController, NavParams, PopoverController, AlertController, ModalController, ?).
The ? should be the “ViewNavigatorService”.
Why this error happens?
Is there a better way to do this? What are you using when your app uses dynamic navigation?