Vue.js custom component with HTML ` internally? If so, can you show what you've tried? – Phil Oct 23 '19 at 05:58

  • Hi, thanks for your quick answer and sorry for my not native English, I might have not explain myself well. I am trying to create a custom Select component only. I'm going to edit my first message to add one of the many code I tried. – Claire Oct 23 '19 at 06:03
  • 1 Answers1

    1

    v-model is syntax sugar. By default, the value is a prop that has the name value, and it changes (two-way-binding) whenever the event input is emitted.

    Also, v-model is bound on the select element, not option.

    Your code can be modified as such:

    <template>
      <select :value="value" @input="clicked">
        <option
          v-for="option in options"
          :key="option.label"
          :value="option.value"
        >
          {{ option.label }}
        </option>
      </select>
    </template>
    
    <script>
      export default {
        props: {
          value: {
            required: true
          },
          options: {
            type: Array,
            required: true
          }
        },
        methods: {
          clicked($event) {
            this.$emit('input', $event.target.value);
          }
        }
      };
    </script>
    

    Documentation here: https://v2.vuejs.org/v2/guide/components.html#Using-v-model-on-Components

    You can also change the prop name and event name that v-model uses, see: https://v2.vuejs.org/v2/guide/components-custom-events.html#Customizing-Component-v-model

    tony19
    • 125,647
    • 18
    • 229
    • 307
    Jasmonate
    • 752
    • 1
    • 8
    • 18
    • Hi @Jasmonate and thanks for your answer. I edited the code in my first message but it still doesn't work. I probably don't understand the links between `option.value` in option, `value` in props and `$event` :s Does `$event` shoot the `option.value` or am I suppose to do something more in the method? In the Vue.js example they wrote `$event.target.value` If I do the html-select tag directly in the parent component, the `selectedOption` is updated correctly. – Claire Oct 23 '19 at 07:11
    • Hi, sorry for the confusion, I haven't been using native `select` in a while. I've fixed my answer. – Jasmonate Oct 23 '19 at 07:18
    • 1
      No worries @Jasmonate, thanks for taking the time to help me! So I modified my code with your corrections and yes it works. But if I want to have a W3C valid code it doesn't accept value on select tag. `Error: Attribute value not allowed on element select at this point. – Claire Oct 23 '19 at 07:34
    • Vue uses virtual dom, and should strip component properties from HTML element attributes if they are declared as props. I've tried to reproduce this in my local environment (vue@2.6.0) and the `value` attribute was stripped from ` – Jasmonate Oct 23 '19 at 07:57
    • If you want to strip all additional attributes, you can try this: https://vuejs.org/v2/api/#inheritAttrs example: `export default { inheritAttrs: false, data() { ... } }` – Jasmonate Oct 23 '19 at 07:58
    • Hi @Jasmonate, thanks a lot for trying on your side. So it appears that I might have a problem in the all project (using Nuxt 2.10.2, don't know if it can change something). Because `value` is in `props` for the `custom-select` but when I look at the source code after a `npm run dev` I still have the attribute `value` in the `select` tag but not the `@input`. I added `inheritAttrs: false` as you suggest on the `custom-select` but still got the `value` attribute! – Claire Oct 23 '19 at 13:42
    • I checked again today. If I inspect the element in browser, you're correct `value` is no longer in the ` – Claire Oct 24 '19 at 09:08