How to bind components with vue js?

后端 未结 1 1572
隐瞒了意图╮
隐瞒了意图╮ 2021-01-23 23:33

I have form and select components. In fact things are simple: I need two binding model.

The parent component:

    Vue.component(\'some-form\', {
      te         


        
相关标签:
1条回答
  • 2021-01-24 00:21

    Here is your countries component updated to support v-model.

    Vue.component('countries', {
      template: `
      <label for="">
        <select v-model="countryName">
          <option value="0">Select the country!</option>
          <option v-for="item in items" v-bind:value="item.name">{{ item.name }}</option>
        </select>
      </label>
      `,
      data: function () {
        return {
          items: {
            "0": {
              "id": 3,
              "name": "Afghanistan"          
            },
            "1": {
              "id": 4,
              "name": "Afghanistan2"          
            },
            "2": {
              "id": 5,
              "name": "Afghanistan3"          
            }
          },
        }
      },
      props: ['value'],
      computed:{
        countryName: {
          get() { return this.value },
          set(v) { this.$emit("input", v) }
        }
      },
    });
    

    v-model is just sugar for setting a value property and listening to the input event. So to support it in any component, the component needs to accept a value property, and emit an input event. Which property and event are used is configurable (documented here).

    0 讨论(0)
提交回复
热议问题