【问题标题】:Vue-MultiSelect: Can't seem to get property from an object during select eventVue-MultiSelect:在选择事件期间似乎无法从对象获取属性
【发布时间】:2019-11-07 19:36:29
【问题描述】:

我正在使用Vue-Multiselect 插件作为输入下拉框,并在客户填写表格时尝试获取他们的姓氏。出于某种原因,我只是得到一个object 作为issue.customer_last_name 属性的值(见下面的截图)。我想得到一个字符串Doe,而不是。

有人有什么想法吗?我的目标是最终通过提交按钮将此数据(POST 请求)提交到远程 api。

复制问题的代码框链接:https://codesandbox.io/s/vue-template-9z1wd?fontsize=14&module=%2Fsrc%2Fcomponents%2FTest.vue

完整代码:

<template>
  <div>
    <label for="customer_last_name_input">Last Name:</label>
    <multiselect
      id="customer_last_name_input"
      v-model="issue.customer_last_name"
      :options="customers"
      placeholder="Select an existing customer or type to add a new one"
      :custom-label="customerSelectName"
      label="lastname"
      track-by="uid"
      :close-on-select="true"
      @select="onSelect"
    ></multiselect>
    <br>
    <!-- <input id="customer_last_name_input" type="text" class="form-control" v-model="issue.customer_last_name" placeholder required /> -->
    <div>
      <label for="customer_first_name_input">First Name:</label>
      <input
        id="customer_first_name_input"
        type="text"
        v-model="issue.customer_first_name"
        placeholder
        required
      >
    </div>
  </div>
</template>

<script>
import Multiselect from "vue-multiselect";
export default {
  name: "test",
  components: {
    Multiselect
  },
  data() {
    return {
      customers: [
        {
          uid: "1",
          firstname: "John",
          lastname: "Doe",
          email: "johndoe@aol.com",
          phone: null,
          c_organization: "ACME",
          c_title: null
        },
        {
          uid: "2",
          firstname: "Mary",
          lastname: "Smith",
          email: "msmith@aol.com",
          phone: null,
          c_organization: "NBA",
          c_title: "Miss"
        }
      ],
      issue: {
        customer_first_name: "",
        customer_last_name: ""
        //customer_email: ""
      }
    };
  },
  // async created() {
  //   await this.getCustomers()
  // },
  methods: {
    customerSelectName(option) {
      //return `${option.lastname}, ${option.firstname}`
      return `${option.lastname}, ${option.firstname}`;
    },
    onSelect(option) {
      console.log(option.lastname);
      this.issue.customer_last_name = option.lastname;
      this.issue.customer_first_name = option.firstname;
      // this.issue.customer_email = option.email
    }
  }
};
</script>

【问题讨论】:

    标签: vue.js vue-multiselect


    【解决方案1】:

    问题在于多选本身的v-model="issue.customer_last_name"。这将多选组件的选择设置为用户选择的整个对象,而不仅仅是姓氏。

    由于您使用 @select="onSelect" 进行所有必要的更新,只需删除 v-model(代码沙箱中的第 6 行)即可完成所有设置。

    编辑添加:

    当您看到无用的 [object Object] 时,将其渲染到其他地方可能会很有用,在那里它更有可能被完全扩展。这在某种程度上取决于浏览器,但添加 &lt;div&gt;{{issue.customer_last_name}}&lt;/div&gt; 是一个有用的调试工具。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-27
      • 1970-01-01
      • 2017-10-17
      • 1970-01-01
      相关资源
      最近更新 更多