Search code examples
angularangular2-routinges6-promiseangular2-resolve

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

    })
}

Solution

  • It is

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

    No Promise.resolve is necessary.