Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对在JavaScript ECMA2015中导入或创建的模块作出反应_Javascript_Constructor_Module_Ecmascript 6 - Fatal编程技术网

对在JavaScript ECMA2015中导入或创建的模块作出反应

对在JavaScript ECMA2015中导入或创建的模块作出反应,javascript,constructor,module,ecmascript-6,Javascript,Constructor,Module,Ecmascript 6,假设我们有一个这样声明的模块 import Stuff from "./stuff"; export default { things: { a: 1 }, stuff: Stuff } 然后,我们将它提取到另一个模块并使用,如下所示 import Donkey from "./stuff-and-things" console.log(Donkey.things.a); console.log(Donkey.stuff.whatever-field); 我希望能够对将前者导入后者做

假设我们有一个这样声明的模块

import Stuff from "./stuff";
export default {
  things: { a: 1 },
  stuff: Stuff
}
然后,我们将它提取到另一个模块并使用,如下所示

import Donkey from "./stuff-and-things"
console.log(Donkey.things.a);
console.log(Donkey.stuff.whatever-field);
我希望能够对将前者导入后者做出反应,以便在创建(添加、合并、注入任何调用)时启动一个函数

类似于这个伪代码

import Stuff from "./stuff";
export default {
  constructor: function(){ console.log("I'm alive!"); }
  things: { a: 1 },
  stuff: Stuff
}

有可能吗?我已经胡思乱想了一段时间,但我什么也没找到——没有关于如何去做的建议,也没有否认。可能是因为不了解主题,我使用了错误的术语。

在导入模块时,没有办法得到通知。我想你能做的是导出一个函数。然后,为了对该模块做任何有用的事情,必须首先调用该函数

例如:

// stuff-and-things
import Stuff from "./stuff";

const api = {
  things: { a: 1 },
  stuff: Stuff
};
export default function() {
  console.log("I'm alive!");
  return api;
};

// other
import stuffAndThings from "./stuff-and-things"
const Donkey = stuffAndThings();
console.log(Donkey.things.a);
console.log(Donkey.stuff.whateverField);

在导入模块时,没有一种方法可以得到通知。我想你能做的是导出一个函数。然后,为了对该模块做任何有用的事情,必须首先调用该函数

例如:

// stuff-and-things
import Stuff from "./stuff";

const api = {
  things: { a: 1 },
  stuff: Stuff
};
export default function() {
  console.log("I'm alive!");
  return api;
};

// other
import stuffAndThings from "./stuff-and-things"
const Donkey = stuffAndThings();
console.log(Donkey.things.a);
console.log(Donkey.stuff.whateverField);