Astro 组件之间共享状态
在构建 Astro 网站时,你可能需要跨组件共享状态。Astro 建议使用 纳米存储 来共享客户端存储。
¥When building an Astro website, you may need to share state across components. Astro recommends the use of Nano Stores for shared client storage.
秘诀
标题部分 秘诀¥Recipe
-
Install Nano Stores:
-
Create a store. In this example, the store tracks whether a dialog is open or not:
-
Import and use the store in a
<script>
tag in the components that will share state.The following
Button
andDialog
components each use the sharedisOpen
state to control whether a particular<div>
is hidden or displayed:
资源
标题部分 资源¥Resources
Recipes