My application has some pages that need to share data between the components, and it is working, but during the test with a customer today I caught an unexpected situation. The user placed a “/” in a field, resulting in a failure.
This issue can only be avoided by creating a wildcard route that could match this request.
As you may not know upfront how many mistakes the user will make, it is basically a 404 route you need to develop and then maybe use that route to infer what the best route should have been.
Check the angular docs on routing on how to do this.
At least that i what I would do.
As to create a way to share data between components in order to avoid sending data between routes, the alternative is to use a service and state management.
private Op = new BehaviorSubject<any>("");
private ID = new BehaviorSubject<any>("");
private Name = new BehaviorSubject<any>("");
get _Op() {
return this.Op.asObservable();
}
get _ID() {
return this.ID.asObservable();
}
get _Name() {
return this.Name.asObservable();
}
MarkingCreateUpdate(op: any, id: any, name: any) {
this.Op.next(op);
this.ID.next(id);
this.Name.next(name);
this._Router.navigate(['marking-create-update']);
}
As convention If u want to do use underscore to mark private things, then I would do the opposite of what u do: the getters without underscore and the behaviorsubject properties with
It would make your code of the consumers a bit more readable
I prefer to add a dollar sign to the functions returning observables. As they are so special compared to others. But that is just a preference
And as a next step try (read: u must) get rid of the any keyword. Better to type everything with an interface to avoid bugs
The way u r using any right now, u might as well remove them everywhere including the <…> typings.
Now you are using typescript language to show the proverbial finger to it
In addition to @Tommertom’s comments, you don’t need asObservable. You can simply return a Subject from a function declared to return an Observable and achieve the same effect. You are also leaking subscriptions: the way I deal with this is ngneat/until-destroy.
I’d love to take credit for it, but frankly I stole the idea from the RxJS internals. See this discussion for a PR that tried to basically add asObservable in a bunch of places and got shot down because it reduces performance without actually adding any additional safety.