Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

In Angular 2 I have a MyObjectComponent that shows an array of myObjects. I get this myObjects from a MyObjectService which is invoked by @CanActivate.

@CanActivate((next: ComponentInstruction, previous: ComponentInstruction) => {

    let myObjectService: MyObjectService = //get my ObjectService here....

    return new Promise((resolve) => {
        myObjectService.getMyObjects()
            .subscribe((myObjects) => {
               next.params['myObjects'] = myObjects;
               resolve(true);
             });
    })

In my component I get the data via

routerOnActivate(next) {
    this.myComponentObjectVariable = next.params.myObjects;
}

This works, but only if the myObjects that I add to next.params['myObjects'] is a string.

If I add an object I will get a

Typescript error: TS2322: Type 'any[]' is not assignable to type 'string'.

However, the resulting JavaScript works as expected. But I want to get rid of the Typescript error message.

The error results because of the definition of the params object in ComponentInstruction:

declare class ComponentInstruction {
    ... some other data ...
    params: {
        [key: string]: string;
    }; 
}

Of course I can change the class ComponentInstruction but that would be a bit ugly.

Does anyone have an idea how to get rid of this error message?

share|improve this question

params is supposed to be treated as immutable.

I'd suggest you use the approach demonstrated in the Plunker linked to in this comment https://github.com/angular/angular/issues/4112#issuecomment-153811572 and pass the value using a shared service instead of route params.

An alternative way is to use

_router.navigate(...) and pass custom route parameters there.

For this case you also would need the approach demonstrated in the Plunker to get the applications Router instance.

share|improve this answer
    
My aim is to resolve data before routing. This was possible in Angular1 with resolve in the route definition. So I have to create a new service. This new service receives the data from @CanActivate() and @CanActivate() resolves the promise when the process of storing has finished. Then I can use in MyComponent the new service without waiting for the data. Isn't this a bit overkill? If I have different types of MyObjects in my application, I will have to write for every type a special service to share the data. Or a general service for all MyObjects without type safety. – Rose Nettoyeur Apr 23 '16 at 12:09
    
The Angular team is aware that the router has serious limitations. The router is work in progress. There are lots of changes to be expected. – Günter Zöchbauer Apr 23 '16 at 12:12
1  
Thank you for your answer. I tried the solution with a service to share the data. I hope there will be another solution available soon. – Rose Nettoyeur Apr 23 '16 at 12:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.