![](/img/trans.png)
[英]ReferenceError: can't access lexical declaration`X' before initialization
[英]Node.js can't access lexical declaration 'x' before initialization and global variable problem
我正在制作的 node.js 應用程序有問題。 這是我第一次使用 node.js 所以我知道會有問題但到目前為止我無法解決這個問題。 我想在單擊站點上的按鈕后運行腳本,但在初始化錯誤之前無法訪問詞法聲明“日志” ,我認為這是因為我正在使用全局變量,因為控制台也顯示此錯誤消息需要沒有定義。
這是我導出的腳本的代碼:
let login
{
login: Cat;
haslo: 123;
}
function Logowanie(x, y){
if (x == login.login && y == login.haslo){
const zmianaTla = document.getElementById('overlay');
const udanyLogin = document.getElementById('logon');
zmianaTla.classList.remove('active');
udanyLogin.remove();
}
else
alert("Niepoprawny login lub haslo");
}
module.exports = new Logowanie;
主腳本:
const log=require('./login.js');
x=document.getElementById('login').value;
y=document.getElementById('haslo').value;
log(x, y)
和 html 部分:
<script src="script.js"></script>
<div class="logon" id="logon">
<h1>Logowanie</h1>
<label for="Log"><b>Login</b></label>
<input type="text" id="login" name="login" required>
<label for="Haslo"><b>Hasło</b></label>
<input type="password" id="haslo" name="haslo" required>
<button type="submit" class="b1" onClick="log()"> Login </button>
</div>
<div class="active" id="overlay"></div>
我嘗試使用其他用戶遇到的類似問題的解決方案來解決它,但他們沒有工作,所以我最終決定自己尋求幫助。
我可能在這里誤解了你,如果是這樣,我很抱歉,但也許這是一些幫助。
好的,您是在瀏覽器中運行它,還是使用 Node.JS(比如通過命令行)?
因為我相信像document.getElement
這樣的調用是特定於瀏覽器的。 可能這就是為什么您需要未定義錯誤的原因。 require
不在瀏覽器中實現,僅在 Node.JS 上實現。 我認為瀏覽器模塊語法不同。
也許這篇文章可以幫助了解 Node.JS 和 JS 在瀏覽器中的區別
其次,我認為在初始化錯誤之前無法訪問詞法聲明“日志”將是由不正確地使用模塊導出引起的。
您的login.js文件可能也需要更改module.exports = new Logowanie;
到module.exports = Logowanie;
很確定new
關鍵字僅用於實例化類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.