Haedrian
Haedrian

Reputation: 4328

Using Resolve to stop a route change

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?
        }

    })
}

Upvotes: 3

Views: 1515

Answers (2)

Rene Juuse
Rene Juuse

Reputation: 585

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

Upvotes: 0

Estus Flask
Estus Flask

Reputation: 222498

It is

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

No Promise.resolve is necessary.

Upvotes: 3

Related Questions