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;
iPhoneJavaDev
  • 821
  • 5
  • 33
  • 78
  • Does this answer your question? [Export more than one variable in ES6?](https://stackoverflow.com/questions/34645731/export-more-than-one-variable-in-es6) – Liam May 28 '20 at 08:38

1 Answers1

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'
Daniel Rothig
  • 696
  • 3
  • 10
  • 1
    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';` – iPhoneJavaDev Nov 07 '18 at 08:50