1

使用 Vue-Multiselect 的新手。我正在使用 axios 从 JSON 占位符执行 GET 请求以进行测试。

如何让标题和帖子 ID 显示在我的下拉列表中?

现在,我的选择框中只显示了 [Object Object] - [title]。

在此处输入图像描述

    <!-- Vue component -->
<template>
  <div>
    <multiselect v-model='value' :options='posts' :custom-label='postWithTitle' placeholder='Select one' label='title' track-by='id'></multiselect>
    {{ value  }}
  </div>
</template>

<script>
import Multiselect from "vue-multiselect";
import axios from "axios";

export default {
  // OR register locally
  components: { Multiselect },
  data() {
    return {
      value: null,
      posts: []
    };
  },
  created() {
    this.getPosts();
  },
  methods: {
    getPosts() {
      axios
        .get("https://jsonplaceholder.typicode.com/posts")
        .then(response => {
          // eslint-disable-next-line
          console.log(response);
          this.posts = response.data;
        })
        .catch(error => {
          // eslint-disable-next-line
          console.log(error);
        });
    },
    postWithTitle(id, title) {
      return `${id} - [${title}]`;
    }
  }
};
</script>
4

1 回答 1

0

使固定:

  postWithTitle(option) {
     return `${option.id} - [${option.title}]`;
  }

解释:

console.log当我简单地进入postWithTitle函数时,我看到了这一点:

自定义custom-label属性正在接受一个只接受一个参数的回调。该参数是整个对象-数组option的单个条目。posts

于 2019-02-12T00:35:40.807 回答