I'd like to have an instance of a slugger across the instances of a single component in as page. Is there a way to do this?
#How to get persistent variables for a component
5 messages · Page 1 of 1 (latest)
Node.js modules are singletons by default. I would put the constructor call in a separate .ts/.js file and then just import the slugger in each frontmatter. This will automatically give you the same instance of the object
// slugger.ts
const slugger = new GithubSlugger();
export default slugger;
// Component.astro
---
import slugger from "path/to/slugger.ts";
---
<h1 id={slugger.slug("abc")}>ABC</h1>
got it, ty!
No worries 🙂