Akash
Akash

Reputation: 988

executing resolver on demand in angular 5

I am working on an angular app which I recently converted into angular 5 from angular 4.2( although I am not using any feature of angular 5 yet).

I need to execute an resolver on a particular route on demand ( from the code when a certain condition met). for example

if ( a === b) {
     //execute resolver
  }

I am not sure, what is the right way of doing that as resolver is a class just like any other typescript class.

Upvotes: 1

Views: 768

Answers (2)

Akash
Akash

Reputation: 988

I actually end-up doing by updating my route config and putting an condition in the resolver's resolve method itself.

Upvotes: 1

BSchnitzel
BSchnitzel

Reputation: 158

I'll give it a shot. Just a generalization since you haven't posted any actual code. I used a simple ternary to get a task done earlier this year

app.module.ts provider excerpt of Resolver

  {
        provide: 'conditionResolver', useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => {
            //Something like this
            window.open((route.data as any).otherUrl);
        }
    }

app.router.ts route excerpt of Implementation

    path: 'SomeComponentURL', component: SomeComponent, resolve: {
        url: 'conditionResolver'
    },
    data: {
        condition: (someCondition != '#/SomeComponent')
            ? 'doSomething()' : orDoSomethingElse()
    }

Upvotes: 1

Related Questions