1

I have a the following sample.ts:

class Sample {
    var1;
    var2;
    ...
}
export default new Sample();

In another class, i imported it using:

import sample from './sample';

And use it as:

sample.var1;

But I would like to access var2 without using sample.var2. I thought of exporting var2 as well, but I'm not sure if that is possible. I want something like below, so I can use var2 directly when i import the file.

class Sample {...}
export default new Sample(), var2;
1

1 Answer 1

2

Replace your export statement with

const sampleToExport = new Sample();

export sample = sampleToExport;
export var2 = sampleToExport.var2;

You can then import it like this:

import { sample, var2 } from './sample'
Sign up to request clarification or add additional context in comments.

1 Comment

actually i can't export the variable directly, what i did is const sample = new Sample(); export const samp = sample, variable2 = sample.var2; Then import {samp, variable2} from './sample';

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.