1

In vue3 I am passing an array of options from parent component to child component in order to use it as options for a select. At the moment, I am not able to use it to initialize my select.

Here is the child component SmartNumberInput

   <template>
  <div>
    <div>Selected: {{ selected }} Initial:{{ initial }}</div>
    {{ options }}

    <div v-for="option in options" :key="option.value">
      {{ option.text }}
    </div>

    <input type="number" v-model="input_value" />


    <select v-model="selected">
      <option
        v-for="option in options"
        :value="option.value"
        :key="option.value"
      >
        {{ option.text }}
      </option>
</select>
    
  </div>
</template>
<script>
export default {
  props: ["initial", "options"],
  data() {
    return {
      selected: "",
      input_value: "",
    };
  },
};
</script>

Here is the parent component

<template>
  <div>
    <h1>Hi! I am the root component</h1>
    <div class="smart-number-input">
      <smart-number-input
        initial="B"
        options="[{text:'Liters',value:'A'},{text:'Gallons',value:'B'},{text:'Pints',value:'C'}]"
      />
    </div>
  </div>
</template>
  
<script>
import SmartNumberInput from "./SmartNumberInput.vue";
export default {
  data() {
    return {
      initial: "salut",
    };
  },
  components: { SmartNumberInput },
};
</script>
<style>
.smart-number-input {
  width: 100%;
  background:#EEE;
}
</style>

In the result I get (see picture) there is no option visible in the select though when the small arrow is clicked it expands with a long empty list. The {{options}} statement in the child displays what I pass as prop i.e. an array of objects but nothing is displayed in the div where I use a v-for loop. When I declare the options as data in the child both loops (div and select) work fine.

Could somebody explain what is wrong in the way I pass or use the array of options ?

enter image description here

1 Answer 1

1

change options to :options (add colon symbol)

.

if you not put colon, it will treat the value as a String...

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.