是否可以在模块文件中访问这些功能?
不,它们在主模块的范围内。您可以导出它们并将它们导入到其他模块中,但这是不可取的-它将在您的入口点上创建循环依赖关系。
而不是在每个模块的顶部重写它们?
把它们放在自己的模块中,然后导入到任何地方。
// dom_helpers.js
/* Selector shortcuts - mimic jQuery style selectors but using more modern, standard code */
export const $ = (selector, scope = document) => scope.querySelector(selector);
export const $$ = (selector, scope = document) => scope.querySelectorAll(selector);
export const on = (el, type, listener) => el.addEventListener(type, listener);
// master.js
// Load components
import initBGVideo from './BGVideo';
import initFieldLabel from './FieldLabel';
import {$$, on} from './dom_helpers';
// Invoke components
on(document, 'DOMContentLoaded', e => {
for (const el of $$('[data-background-video]')) {
initBGVideo(el);
}
for (const el of $$('.c-form__item')) {
initFieldLabel(el);
}
});
// BGVideo.js
import {$, $$, on} from './dom_helpers';
export default function init(el) {
â¦
}