1

I am trying to run a simple pug for loop inside of my nuxt app.

I installed https://www.npmjs.com/package/vue-pug-loader by running the commands, and the syntax seems to work:

<template lang="pug">
  if (10 / 2) == 5
    p Hello world!
</template>

This shows hello world correctly, so does a for loop like

<template lang="pug">
  for item in ["1", "2", "3"]
    p #{item}
</template>

But whenever I try and use a variable from my script It throws an error:
Cannot read properties of undefined (reading 'length')

<template lang="pug">
  for item in pages // error triggers here
    p #{item}
</template>

<script>
let pages = ["1", "2", "3"]
</script>

<style>
</style>

What am I doing wrong?

1 Answer 1

1

It doesn't relates to Pug.

<script>
let pages = ["1", "2", "3"]
</script>

<script> and <script setup> are different, see API Styles. The top-level variables in <script> won't be exposed to <template>.

You can simply change <script> to <script setup>, or:

<script>
export default defineComponent({
  setup() {
    let pages = ["1", "2", "3"]
    return { pages }
  }
})
</script>

Or using Options API:

<script>
export default defineComponent({
  data() {
    return { pages: ["1", "2", "3"] }
  }
})
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

That doesn't seem to work strangely, I tried all of your methods but its still the same error
@mrolland Please provide a minimal reproducible example using StackBlitz or CodeSandbox

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.