import { Component } from '@angular/core';
import { NavParams } from '@ionic/angular';
@Component({...})
export class MyPage {
constructor(
navParams: NavParams
) {
console.log(navParams.get('foo'));
}
}
Result: No provider for NavParams!
My current solution is to use router.getCurrentNavigation().extras.state and pass the data by extras state. But maybe there is a better solution? I want to pass data as body. Not as query params in the url.
My current thinking on this topic is that it isn’t worth trying to jam application state into the router, so I just let the router route and manage application state completely separately, in one (or several) services specifically dedicated to that task. Since anything done in the router must be super-generic, it’s not going to be as feature-rich or easy to read as a dedicated service with domain-specific interfaces and descriptive method names. See this post for an example of what I’m talking about.
I think getter and setter functions like that violate the principle of least surprise, although perhaps that is less true for people with extensive JavaScript experience. When I do something like this.fruit = "apple" or let fruit = this.fruits.fruit I’m not expecting anything to happen aside from a mere assignment; I’m not wanting either the potential overhead or unexpected side effects of a function call.
So I would just do this (with the caveat that data is a terrible name in a real application; it’s worth taking the time to use something descriptive):
private data = new BehaviorSubject<Data>({});
getData(): Observable<Data> { return data; }