簡體   English   中英

如何從 JS 模塊 class 中調用外部 scope function

[英]How to call an outer scope function from within a JS module class

為什么這會引發錯誤,從模塊 class 中調用外部(全局范圍)function 的正確方法是什么?

//models.js
export default class Model {
    log() {
        gLog();     //Uncaught ReferenceError: gLog is not defined
    }
}

//main.js
import Model from './models.js';
let m = new Model();
m.log();

function gLog() {
    console.log(1);
}

一個模塊無權訪問另一個模塊中聲明的對象,除非該模塊導出這些對象。 您應該在您的models模塊中包含gLog (更簡單的方法)或將其導出到您的main.js中,然后在models中導入main.js

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM