基本ES6 Javascript插件 – 在函数之间重用变量

前端之家收集整理的这篇文章主要介绍了基本ES6 Javascript插件 – 在函数之间重用变量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试构建一个基本的JS插件,可以在单击事件后调用以禁用按钮(以防止用户触发多个API调用)并提供有关正在加载/发生的事情的反馈.以下是它的外观:

enter image description here

这在个人基础上很有用,但我想将其重新编写为插件,以便我可以在整个网站上重复使用它.

这是来自文件loader.plugin.js的JS的缩减版本.

  1. let originalBtnText;
  2. export function showBtnLoader(btn,loadingText) {
  3. const clickedBtn = btn;
  4. const spinner = document.createElement('div');
  5. spinner.classList.add('spin-loader');
  6. originalBtnText = clickedBtn.textContent;
  7. clickedBtn.textContent = loadingText;
  8. clickedBtn.appendChild(spinner);
  9. clickedBtn.setAttribute('disabled',true);
  10. clickedBtn.classList.add('loading');
  11. return this;
  12. }
  13. export function hideBtnLoader(btn) {
  14. const clickedBtn = btn.target;
  15. clickedBtn.textContent = originalBtnText;
  16. clickedBtn.removeAttribute('disabled');
  17. clickedBtn.classList.remove('loading');
  18. return this;
  19. }
  20. export function btnLoader() {
  21. showBtnLoader();
  22. hideBtnLoader();
  23. }

这是一个我想如何使用它的例子.

  1. import btnLoader from 'loaderPlugin';
  2. const signupBtn = document.getElementById('signup-btn');
  3. signupBtn.addEventListener('click',function(e) {
  4. e.preventDefault();
  5. btnLoader.showBtnLoader(signupBtn,'Validating');
  6. // Call API here
  7. });
  8. // Following API response
  9. hideBtnLoader(signupBtn);

我的问题是我想从showBtnLoader函数存储originalBtnText,然后在hideBtnLoader函数中使用该变量.我当然可以用不同的方式实现这一点(例如将值作为数据属性添加并稍后抓取它)但我想知道是否有一种简单的方法.

我遇到的另一个问题是我不知道调用每个函数的正确方法以及我是否正确导入它.我尝试了以下内容.

  1. btnLoader.showBtnLoader(signupBtn,'Validating');
  2. btnLoader(showBtnLoader(signupBtn,'Validating'));
  3. showBtnLoader(signupBtn,'Validating');

但是我收到以下错误

  1. Uncaught ReferenceError: showBtnLoader is not defined
  2. at HTMLButtonElement.

我已经阅读了一些很好的文章和SO答案,如http://2ality.com/2014/09/es6-modules-final.htmlES6 export default with multiple functions referring to each other,但我对这种“正确”的方法感到有些困惑,以使其可重复使用.

任何指针都将非常感激.

最佳答案
我会导出一个创建一个具有show和hide函数的对象的函数,如下所示:

  1. export default function(btn,loadingText) {
  2. function show() {
  3. const clickedBtn = btn;
  4. const spinner = document.createElement('div');
  5. spinner.classList.add('spin-loader');
  6. originalBtnText = clickedBtn.textContent;
  7. clickedBtn.textContent = loadingText;
  8. clickedBtn.appendChild(spinner);
  9. clickedBtn.setAttribute('disabled',true);
  10. clickedBtn.classList.add('loading');
  11. }
  12. function hide() {
  13. const clickedBtn = btn.target;
  14. clickedBtn.textContent = originalBtnText;
  15. clickedBtn.removeAttribute('disabled');
  16. clickedBtn.classList.remove('loading');
  17. }
  18. return {
  19. show,hide,};
  20. }

然后,使用它:

  1. import btnLoader from 'btnloader';
  2. const signupBtn = document.getElementById('signup-btn');
  3. const signupLoader = btnLoader( signupBtn,'Validating' );
  4. signupBtn.addEventListener('click',function(e) {
  5. e.preventDefault();
  6. signupLoader.show();
  7. // Call API here
  8. });
  9. // Following API response
  10. signupLoader.hide();

如果您需要将其从显示它的位置的其他文件中隐藏,则可以导出实例:

  1. export const signupLoader = btnLoader( signupBtn,'Validating' );

然后导入它.

  1. import { signupLoader } from 'signupform';
  2. function handleApi() {
  3. signupLoader.hide();
  4. }

猜你在找的JavaScript相关文章