Ted Nyberg Ted Nyberg - 3 months ago 26
TypeScript Question

Access imported type's static members in Aurelia view

I'm using Aurelia with TypeScript.

I have a simple type with static variables like so:

export class MyModule {
static foo = false;
}


I have an Aurelia view model like the following:

import {MyModule} from 'my-module';

export class MyViewModel{
constructor() {
MyModule.foo = true;
console.log(MyModule.foo); // Outputs true
}
}


I would like to access this static member in my view, like the following:

<p if.bind="MyModule.foo">
This is displayed since foo is true.
</p>


However, in the view, it seems I'm unable to get the static members of the type imported in the view model.

I must be misunderstanding (at least) one of the Aurelia concepts. :)

Answer

That's because MyModule is not a member of your class.

Try this:

import {MyModule} from 'my-module';

export class MyViewModel{
    constructor() {
       this.myModule = MyModule;
       this.myModule.foo = true;
    }
}

Then

<p if.bind="myModule.foo">
   This is displayed since foo is true.
</p>