Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 access exported 'default' instance from same file

I was wondering, when you export some data using the ES6 keyword export like so :

export default {
  data: "Hello !"
}

How can you then from the same file, access that exact same exported data ?

EDIT: Of course, without declaring it before exporting the variable...

like image 485
Scaraux Avatar asked Jan 04 '23 16:01

Scaraux


2 Answers

If you structure your file like that, you cannot.

Usually, you define functions and data that you want to expose before the export statement, and then reference them when you build the exported object/function.

Bad way:

export default {
    data: 'Hello!',
    myFn: function (str) {
        console.log(str);
    }
}

Good way:

var data = 'Hello!';

var myFn = function (str) {
    console.log(str);
};

// code that uses data and myFn

// shorthand for { data: data, myFn: myFn }
export default { data, myFn };
like image 123
Matt Mokary Avatar answered Jan 13 '23 13:01

Matt Mokary


Try the following

export const data = 'hello';
like image 20
Aluan Haddad Avatar answered Jan 13 '23 13:01

Aluan Haddad