选择后选择vue组件关闭下拉

Selectize vue component closing dropdown after selection

尝试创建一个简单的 selectize vue 组件,但我遇到了问题,每当我 select 一个选项并在组件中使用 v-model 时,下拉列表会自动关闭,同时删除v-model 下拉列表保持打开状态,直到达到指定的最大项目数。

HTML

<div id="app">
  <p>With Model: {{ selected }}</p>
  <selectize v-model="selected" :options="options" data-max-items="2"></selectize>

  <p>Without Model: {{ selected }}</p>
  <selectize :options="options" data-max-items="2"></selectize>
</div>

JS

Vue.component('selectize', {
  props: ['options', 'value'],

  template: '<select><slot></slot></select>',

  mounted() {
    $(this.$el).selectize({
      onInitialize: () => {
        this.$el.selectize.setValue(this.value, true)
      },

      onChange: (value) => {
        this.$emit('input', value)
      },

      ...this.mergedSettings,
    })
  },

  computed: {
    mergedSettings() {
      return $.extend({
        options: this.options,
      }, $(this.$el).data())
    },
  },

  watch: {
    value(value) {
      this.$el.selectize.setValue(value, true)
    },
  },
})

new Vue({
  el: "#app",
  data: {
    options: [
      { value: 1, text: "One"   },
      { value: 2, text: "Two"   },
      { value: 3, text: "Three" },
      { value: 4, text: "Four"  },
    ],

    selected: [],
  },
})

我还创建了一个 jsfiddle:https://jsfiddle.net/uk0g69s4/19/

我对这个解决方案并不感到自豪,但这是我能想到的更好的方法。

创建一个 SELF_CHANGED 属性来检查更改是在内部还是外部触发的...

Vue.component('selectize', {
  props: ['options', 'value'],
  data() {
    return {
      SELF_CHANGED: false
    }
  },
  template: `
    <select>
        <slot></slot>
    </select>
  `,
  mounted() {
    $(this.$el).selectize({
      onInitialize: () => {
        this.$el.selectize.setValue(this.value, true)
      },

      onChange: (value) => {
        this.SELF_CHANGED = true
        this.$emit('input', value)
      },

      ...this.mergedSettings,
    })
  },

  computed: {
    mergedSettings() {
      return $.extend({
        options: this.options,
      }, $(this.$el).data())
    },
  },

  watch: {
    value(value) {
      if (!this.SELF_CHANGED) {
        this.$el.selectize.setValue(value, true)
      }
      this.SELF_CHANGED = false
    },
  },
})

new Vue({
  el: "#app",
  data: {
    options: [{
        value: 1,
        text: "One"
      },
      {
        value: 2,
        text: "Two"
      },
      {
        value: 3,
        text: "Three"
      },
      {
        value: 4,
        text: "Four"
      },
    ],
    selected: [],
  },
})
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.8.5/css/selectize.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sifter/0.5.3/sifter.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/microplugin/0.0.3/microplugin.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/js/selectize.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>

<div id="app">
  <p>With Model: {{ selected }}</p>
  <selectize v-model="selected" :options="options" data-max-items="2"></selectize>
  <p>Without Model: {{ selected }}</p>
  <selectize :options="options" data-max-items="2"></selectize>
</div>