0

I have a project that uses Aurelia framework. I want to make global\static object that should be accessed across couple files. But when I try to access it from a different file it says that my object is undefined. Here is what it looks like:

FirstFile.ts

export function showA() {
    console.log("Changed a to " + a);
}
export var a = 3;

export class FirstFile {
    public ModifyA() {
        a = 7;
        showA();
    }

It says that a = 7. Then I use it in other file like this.

SecondFile.ts

import FirstFile = require("src/FirstFile");
export class SecondFile {
    showA_again() {
        FirstFile.showA();
}

I execute showA_again() in my view file called SecondFile.html

<button click.trigger="showA_again()" class="au-target">Button</button>

When I click button, I see in console that variable "a" is still 3. Is there any way to store variables between files?

2
  • And where do you call the ModifyA()? I can't see this method being called anywhere. Commented Jul 30, 2015 at 15:35
  • This is bad. Don't do this. Add a to the class itself. Commented Aug 3, 2015 at 12:45

1 Answer 1

1

I'd recommend you to inject FirstFile into SecondFile. Now your code has a smell of bad architecture.

To answer your question: probably you are looking for static (playground sample)

export class FirstFile {

    static showA = function() {
        console.log("Changed a to " + FirstFile.a);
    }

    static a = 3;

    public ModifyA() {
        FirstFile.a = 7;
        FirstFile.showA();
    }
}

export class SecondFile {
    showA_again() {
        FirstFile.showA();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Good answer. There are other ways to do this, but I like this one.
@MatthewJamesDavis Could you provide an example? It would be great to see other options.
probably not worth it, this is probably the best way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.