如何将EJS变量传递给Extern JS脚本

发布于 2025-02-11 02:20:23 字数 161 浏览 0 评论 0原文

我的HTML页面上有一个脚本:

<script>
    let a = <%-JSON.stringify(tags)%>
</script>

现在我在main.js文件上需要此变量。但是我不知道如何访问它

i have a script on my html page like that:

<script>
    let a = <%-JSON.stringify(tags)%>
</script>

now I need this variable on my main.js file. But I don't know how I can access it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原野 2025-02-18 02:20:23

您可以使用window.a访问它,只要该脚本标签位于main.js 的脚本标签上方,但这需要一个在开始时设置值的不同方法。

<script>
  window.a = <%-JSON.stringify(tags)%>;
</script>

访问您的代码

let foo = a.bar;

You can access it with window.a, as long as that script tag is above the script tag for main.js, but this requires a different way to set the value at the start.

<script>
  window.a = <%-JSON.stringify(tags)%>;
</script>

access in your code like

let foo = a.bar;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文