Vue-Multiselect 如何使用 Axios?
How to use Axios with Vue-Multiselect?
刚开始使用 Vue-Multiselect。我正在使用 axios 从 JSON 占位符执行 GET 请求以进行测试。
如何让标题和 post ID 显示在我的下拉列表中?
现在,我 [Object Object] - [标题] 显示在我的 select 框中。
<!-- 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>
修复:
postWithTitle(option) {
return `${option.id} - [${option.title}]`;
}
解释:
我看到当我简单地 console.log
进入 postWithTitle
函数时:
自定义 custom-label
属性正在接受一个只接受一个参数的回调。该参数是整个 option
对象 - posts
数组的单个条目。
刚开始使用 Vue-Multiselect。我正在使用 axios 从 JSON 占位符执行 GET 请求以进行测试。
如何让标题和 post ID 显示在我的下拉列表中?
现在,我 [Object Object] - [标题] 显示在我的 select 框中。
<!-- 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>
修复:
postWithTitle(option) {
return `${option.id} - [${option.title}]`;
}
解释:
我看到当我简单地 console.log
进入 postWithTitle
函数时:
自定义 custom-label
属性正在接受一个只接受一个参数的回调。该参数是整个 option
对象 - posts
数组的单个条目。