skip to Main Content

How to pass variable from frontmatter to script in Astro js. Like this:

---
const title = "this is title"
---

some content


<script>

const metatile = title

<script>

2

Answers


  1. You can use the define:vars directive to pass the value to the client bundle.

    ---
    const title = "this is title"
    ---
    
    <script define:vars={{title}}>
      alert(title);
    <script>
    
    Login or Signup to reply.
  2. maybe like this:

       
    
    ---
    const title = "this is title"
    ---
    
    some content
    
    
    <script>
    
    const metatile = '{title}';
    
    <script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search