[英]how to import a class in one .js file into another
我試圖了解如何將 a.js 文件中的 class 導入另一個文件,以便我可以在導入的 class 中運行異步 function。
//file called helloworld.js
class helloworld {
async greetings(){
console.log("hello world")
}
}
export default helloworld;
我正在嘗試在下面的代碼中使用稱為問候的異步 function
import helloworld from './helloworld';
console.log(helloworld.greetings)
我運行時會導致錯誤
節點 helloworld_import.js
1. helloworld
是一個 class 所以你需要先實例化。 那么只有你可以使用它的methods
。
2. greetings
不是一個屬性,它是一個 object 所以需要調用/調用/執行它將()
。
import helloworld from "./helloworld";
const obj = new helloworld();
obj.greetings();
如果您想使用 class 名稱helloworld
訪問,請制作 function greetings
static (為簡單起見,您可以根據需要在單個 js 文件中導入和導出)
class helloworld { static async greetings() { console.log("hello world"); } } helloworld.greetings();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.