Hey there. I’m assembling an app that uses a parent abstract class to handle navigation through to various child page types. Consider the following (simplified) example:
export abstract class BasicPage {
public pages: PageService;
public navCtrl: NavController;
goToPage(id: string) {
let page = this.pages.getPageByID(id);
if (page.type === 'bluePage') this.navCtrl.push(BluePage, { id: id });
if (page.type === 'redPage') this.navCtrl.push(RedPage, { id: id });
}
}
@IonicPage()
@Component({
selector: 'page-blue',
templateUrl: 'blue.html',
})
export class BluePage extends BasicPage {
constructor(public navCtrl: NavController, public pages: PageService) {
super();
}
}
The goal of this is to create a very simple abstract class from which the child pages can navigate to any other page type. For example, if goToPage() is called from the red OR blue page, the parent class BasicPage will correctly determine which page to go to based on the type.
This should work in theory. However, for some reason, I am getting the following error: “Object prototype may only be an Object or null: undefined”. This is fixed by either removing the inheritance in the BluePage, or removing any lines that reference RedPage or BluePage in the BasicPage parent class. Obviously, this defeats the purpose of the inheritance.
I’m sure this is something simple I’m missing based on how inheritance works in TypeScript, but I can’t figure it out or think of an alternative. Abstract classes are my preferred way for handling repetitive code. How would you guys go about fixing this?