Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

    })
}
like image 648
Haedrian Avatar asked Jan 19 '26 13:01

Haedrian


1 Answers

It is

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

No Promise.resolve is necessary.

like image 182
Estus Flask Avatar answered Jan 21 '26 04:01

Estus Flask



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!