简体   繁体   中英

Sharing object between files

Just say i have a file (file A) that does some work for me with reactivity and stores multiple object in a array . How can I share that object with multiple files lets just say file B and file C.

I am using metoerJs by the way thanks.

You can simply export the shared object so that other files could import them:

File foo.js :

const arr = {};

arr.push(123);

export { arr };

Then import in file bar.js :

import { arr } from './foo.js';

This is pretty simple, but there is something you should know. It look like you are using arr to store some of your app data, arr is declared inside foo.js not in a function that means arr will be stored in RAM and not be garbage-collected until your app re-run. That my cause you some problems:

  • If arr is a big array of objects, it will cost you quite a lot of RAM.
  • If your app is in production, re-deploy the app will reset the value of arr

So be careful when using this approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM