簡體   English   中英

在 Webpack 5 中導出對象

[英]Exporting object in Webpack 5

我正在使用 Webpack 構建一個 JS 庫並嘗試導出一個對象。

import jwt_decode from "jwt-decode";

console.log(location.hash.replace('#', ''));

export var upstream = {
  user: {
    getUserDetails: () => {
      if (location.hash) {
        return jwt_decode(location.hash.replace('#', ''));
      } else {
        return null;
      }
    }
  }
}

在我的客戶端代碼中:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>UpStream</title>
</head>

<body>
    <script src="http://localhost:8080/app.js"> <!--server is up, connects fine-->
    </script>
    <script>
        console.log(upstream);
    </script>
</body>

</html>

console.log(); 語句按預期工作,但我無法訪問上游對象。 任何指針?

為了能夠通過window或只是upstream訪問upstream ,您需要確保將導出指定為libraryTarget'window'

希望這有幫助!

暫無
暫無

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

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