简体   繁体   中英

How to import one function instead of whole file?

There is a function i want to export to other modules

index.js

export function addPageSwitchFunctionality() {
  for (let i = 0; i < pages.length; i++)
    pages[i].addEventListener("click", () => {
      if (event.target.innerText === "Home")
        document.location.href = "http://127.0.0.1:5500/public/";
      else
        document.location.href =
          "http://127.0.0.1:5500/public/" +
          event.target.innerText.toLowerCase();
    });
}

addPageSwitchFunctionality();

somePage.js

import { addPageSwitchFunctionality } from "../index.js";

addPageSwitchFunctionality();

My both files have type module in html. When i run somePage.js i get an error that says cannot read property style of null which is a line in index.js. That means my whole file gets imported into somePage.js. How can i prevent this and get only necessary stuff?

update:

Error:

Uncaught TypeError: Cannot read property 'style' of null
    at index.js:165

line 165 index.js:

slideShow.style.backgroundImage = `url(${slideShowImages[slideIndex]})`;

Set slideShow to an object with a style property before or add if (slideShow) at the beginning of index.js:165.

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