1

How to call a variable set in <script> from the template using its string name?

For instance, in the example below, I'd like to call the foo variable using table[0]:

<template>
  <div>
    {{ table[0] }}
    {{ table[1] }}
  </div>
</template>

<script>
export default {
  data() {
    return {
      foo: 'Hello World!',
      bar: '大家好!',
      table: ['foo', 'bar']
    }
  }
}
</script>

1 Answer 1

5

By using $data, like {{ $data[table[0]] }}

Sign up to request clarification or add additional context in comments.

Comments

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.