访问 JS 中导入模块中的私有函数

Access private functions in imported module in JS?

本文关键字:函数 模块 JS 导入 访问      更新时间:2023-09-26

我有定义了一些函数的 Ember 组件,例如:

export default Ember.Component.extend({
  _someFunction: function(){}
});

现在,如果我在其他组件中导入此组件:

import FirstComponent from 'somePath...';

我可以以及如何从FirstComponent调用_someFunction吗?我试过这个FirstComponent._someFunction(),但我得到错误(不是函数)。

我可以在 Ember 组件之外定义此函数并单独导出此函数,但还有其他方法吗?

因为Ember.Component是一个类,并且其中有实例方法_someFunction。必须先创建它的实例才能访问该方法。因此,您应该尝试

const instance = FirstComponent.create();
instance._someFunction();