In my situation, I'm observing the parameters used in the active route to update the content I'm displaying.
In my component, ngOnInit()
function, I have something like this :
this.activeRoute.paramMap
.pipe(
switchMap((params) => {
this.fileName = String(params.get('filePath'));
return this.fileService.checkIfFileExists(this.fileName);
}),
tap((result) => {
if (!result) {
this.router.navigate(['/']);
}
}),
filter((result) => result),
switchMap(() =>
this.modificationService.getFileModifications(this.fileName)
),
tap((result) => {
if (result.allModifications) {
this.modificationList = result.allModifications;
this.noModification = false;
}
})
)
.subscribe();
For more context :
Currently, the behavior is :
I switch from file to file using a menu, I never refresh the page, If I refresh it's working.
I have tried to use the catchError()
from RxJS, the only difference is that the error is no longer in the console, the behavior stays the same.
I used to only have a
this.router.routeReuseStrategy.shouldReuseRoute = () => false;
Instead of the this.activeRoute.paramMap...
it's actually deprecated, I don't want to use it.
The paramMap Observer works perfectly with my other component that doesn't have to manage 404 error
Any help will be welcomed :)
The 404 response from modificationService
is causing your stream to complete. The switchMap
operator switches over to the inner stream, so anything that makes it complete, completes the outer stream. In this case, the 404 response is emitting as an error, which completes the inner stream.
Once a stream completes, its essentially static, so it will ignore any future emissions. The trick to getting around this is to catch the error and return a stream to keep the error from surfacing.
this.activeRoute.paramMap
.pipe(
switchMap((params) => {
this.fileName = String(params.get("filePath"));
return this.fileService.checkIfFileExists(this.fileName);
}),
tap((result) => {
if (!result) {
this.router.navigate(["/"]);
}
}),
filter((result) => result),
switchMap(() =>
this.modificationService
.getFileModifications(this.fileName)
.pipe(
// prevents the complete and returns a new observable
catchError((err) => {
console.error(err);
return of(null);
})
)
),
tap((result) => {
if (result.allModifications) {
this.modificationList = result.allModifications;
this.noModification = false;
}
})
)
.subscribe();