如何在astro JS中使用文档和窗口元素?

发布于 2025-01-15 00:47:53 字数 113 浏览 4 评论 0原文

我想使用窗口或文档返回当前页面的路径默认 Astro.site.pathname 无法正常工作或没有正确的文档。

我的问题是如何在 Astro JS 中正确使用文档或窗口?

提前致谢!

I want to use window or document to return path of the current page default Astro.site.pathname isn't working right or proper documentation isn't available.

My question is how to use document or window properly in Astro JS?

Thanks in advance!

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

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

发布评论

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

评论(4

别低头,皇冠会掉 2025-01-22 00:47:53

如果您想使用 windowdocument 等内容,则需要在 中编写脚本。这样 Astro 就不会接触你的 JS 并将其直接包含在 HTML 中。位于页面顶部 --- --- 之间的 JS 将在构建时执行。

You need to write the script inside <script is:inline></script> if you want to use things like window or document. This way Astro won't touch your JS and include it directly on the HTML. The JS positioned on the top of the page between --- --- will be executed at build time.

云朵有点甜 2025-01-22 00:47:53

您还可以使用 const { pathname } = Astro.url 从 Astro 组件访问当前路径名。

You can also access the current path name from an Astro component using const { pathname } = Astro.url.

无敌元气妹 2025-01-22 00:47:53

文档中提到的脚本标签

https://docs.astro.build/en/guides/client-side-scripts/#using-script-in -astro

内联脚本

如果您不需要

https://docs.astro.build/en/guides/ client-side-scripts/#script-bundling

示例

此示例展示了如何在纯 Astro 中创建多个客户端计数器,这与纯 html 类似。请注意 document.querySelectorAll(".card")document 的用法,

更多详细信息请参见 https://docs.astro.build/en/core-concepts/astro-components/

---
//Astro Front matter => Javascript
---
<!-- html -->

<!-- style -->

<!-- script -->
<script>
    console.log("int cards")
    const cards = document.querySelectorAll(".card")
    cards.forEach(card => {
        const plus = card.querySelector('button.plus')
        const minus = card.querySelector('button.minus')
        const value = card.querySelector(".value")
        plus.onclick  = ()=>{value.textContent = parseInt(value.textContent) + 1}
        minus.onclick = ()=>{value.textContent = parseInt(value.textContent) - 1}
    });
</script>

这里是完整示例存储库项目的链接:

https://github.com/MicroWebStacks/astro-examples#04_client-counters

和这里指向该存储库的直接 StackBlitz 项目的链接

https://stackblitz.com/github/MicroWebStacks/astro-examples/tree/main/04_client-counters

Script tag

<script> tag as mentioned in the documentation is enough for using document and window as it is meant for client side Javascript

https://docs.astro.build/en/guides/client-side-scripts/#using-script-in-astro

Inline Script

<script is:inline> is not recommended if you do not need it because it's intended to avoid bundling your script, which is rarely needed. And if you use the component containing that script, in let's say 10 instances (e,g, 10 Cards), you'll have that script 10 times duplicated, so results in loading performance impact.

https://docs.astro.build/en/guides/client-side-scripts/#script-bundling

Example

This example shows how to create multiple client side counters in pure Astro, which is similar to pure html. Note the usage of document in document.querySelectorAll(".card")

more details in https://docs.astro.build/en/core-concepts/astro-components/

---
//Astro Front matter => Javascript
---
<!-- html -->

<!-- style -->

<!-- script -->
<script>
    console.log("int cards")
    const cards = document.querySelectorAll(".card")
    cards.forEach(card => {
        const plus = card.querySelector('button.plus')
        const minus = card.querySelector('button.minus')
        const value = card.querySelector(".value")
        plus.onclick  = ()=>{value.textContent = parseInt(value.textContent) + 1}
        minus.onclick = ()=>{value.textContent = parseInt(value.textContent) - 1}
    });
</script>

here a link to the full example repo project:

https://github.com/MicroWebStacks/astro-examples#04_client-counters

and here a link to a direct StackBlitz project pointed on that repo

https://stackblitz.com/github/MicroWebStacks/astro-examples/tree/main/04_client-counters

伤痕我心 2025-01-22 00:47:53

默认情况下,Astro 将处理、优化和捆绑它在页面上看到的任何 和 标签。您可以使用 is:inline 指令选择退出此行为。

Docs

您可以使用 is:inline 指令在您的 .astro 组件中如下所示:

---
// static build only
---

<script is:inline>
    console.log(document, window)
</script>

By default, Astro will process, optimize, and bundle any and tags that it sees on the page. You can opt-out of this behavior with the is:inline directive.

Docs

You can use the is:inline directive like so in your .astro components:

---
// static build only
---

<script is:inline>
    console.log(document, window)
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文