3

I have an angular2 app which lists a number of items, which the user can click to get more details about the item.

Now, some of these items I don't actually have details for - which I'll know when I make the service request, and so instead of loading a page with a 404 or whatever, I would like to simply not let him route to that page.

I'm using a Resolve to load the item, so in theory it should be pretty simple, but I can't figure out how to stop the route from changing under certain conditions.

I tried to use Location.back but that didn't seem to work at all.

resolve(route: ActivatedRouteSnapshot): Promise<Article> {
    return this.articleService.getArticle(route.paramMap.get('name')).then((article) => {

        if (article) {
            return Promise.resolve(article);
        }
        else {
            //Stop them?
        }

    })
}
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
Haedrian
  • 4,240
  • 2
  • 32
  • 53

2 Answers2

3

It is

    if (article) {
        return article;
    }
    else {
        return Promise.reject('No articles');
        // or throw ...
    }

No Promise.resolve is necessary.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
0

Another way to prevent navigation is to throw an error from the resolve stream. See https://stackoverflow.com/a/58010984/3936587

Rene Juuse
  • 575
  • 8
  • 16