代码之家  ›  专栏  ›  技术社区  ›  Rashed Hasan Vijayaragavan

如何在VUE.JS 2中在VSET中设置选项值?

  •  0
  • Rashed Hasan Vijayaragavan  · 技术社区  · 6 年前

    我在试着设置 option selected 哪里 选项 价值是 1 . 但是我在使用的时候遇到了麻烦 v-select 来自Vuujs。我就是这么想的-

    <v-select name="branchid" v-model="branchid" 
             :options="branches.map(branches => ({label: branches.label, value: branches.value}))" 
             :selected="branches.value === 1"></v-select>
    

    有人能帮我把 选项 价值 挑选出来的 什么时候 选项 价值是 ?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Marcus    6 年前

    我把你(我想)想做的事情简化为:

    <template>
      <div>
        <div>
          <select v-on:change="select($event);" value="branchid">
            <option disabled value="">Please select one</option>
            <option :selected="branchid === 1">1</option>
            <option :selected="branchid === 2">2</option>
            <option :selected="branchid === 3">3</option>
          </select>
          <span>Selected: {{ branchid }}</span>
          <button v-on:click="selectOne">Select Option 1</button>
        </div>
      </div>
    </template>
    
    <script>
    export default {
      name: "HelloWorld",
      data() {
        return {
          branchid: 0,
          branchidTwo: 1
        };
      },
      methods: {
        select: function(evt) {
        this.branchid = evt.target.value;
      },
        selectOne: function() {
          this.branchid = 1;
        }
      }
    };
    </script>
    

    这不使用v-model模式。文档明确声明,如果使用v-model,类本身将被用作真相的来源,而不是值或选定的。您将看到我添加了一个按钮,它将在select组件上设置selected选项。

    希望能有所帮助。