Within Angular 2, I am using a MyObjectComponent
to display an array of myObjects
. These myObjects
are retrieved from a MyObjectService
, which is called by @CanActivate
.
@CanActivate((next: ComponentInstruction, previous: ComponentInstruction) => {
let myObjectService: MyObjectService = //get MyObjectService instance here....
return new Promise((resolve) => {
myObjectService.getMyObjects()
.subscribe((myObjects) => {
next.params['myObjects'] = myObjects;
resolve(true);
});
})
In the component, I access the data with:
routerOnActivate(next) {
this.myComponentObjectVariable = next.params.myObjects;
}
This setup functions properly, but only when the myObjects
added to next.params['myObjects']
is a string.
If I attempt to add an object, I encounter a TypeScript error:
Typescript error: TS2322: Type 'any[]' is not assignable to type 'string'.
The JavaScript output works fine, but eliminating the Typescript error is desired. This issue arises due to the params
object's definition in ComponentInstruction
:
declare class ComponentInstruction {
... some other data ...
params: {
[key: string]: string;
};
}
One potential solution could involve modifying the ComponentInstruction
class, although that may seem cumbersome. Any suggestions on resolving this error message would be appreciated.