vue中如何在v-select中传递值
how to pass value in v-select in vue
我是 vue(2.5 版)的新手,在我的项目中安装了 v-select 我浏览了文档,但对一些事情感到困惑
这是我的代码
<template>
<div class="wrapper">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="name">Category</label>
<v-select label="category_desc" options="category"></v-select>
</div>
</div>
</div>
<button type="button" variant="primary" class="px-4" @click.prevent="next()">next</button>
</div>
</template>
<script>
export default {
name: 'Addproduct',
data () {
return {
category:[]
}
},
mounted() {
this.$http.get('http://localhost:3000/api/categories') .then(function (response) {
console.log(response.data);
this.category=response.data
})
.catch(function (error) {
console.log("error.response");
});
},
方法:{
下一个(){
// console.log('value of v-selection not option' ) 例如 id 1 有 'country' 所以我想在方法 next() 中使用 id 1 即在 console.log
}
}
现在我的问题是我如何将这个 axios 响应的成功值传递给 v-select 选项,其次我如何获得 v-select 的 selected 值例如;- 当用户单击下一步按钮我如何获得在 v-select
中 selected 的值
我们是在谈论 this select component 吗?最简单的用法是在 v-select 上放置一个 v-model 属性。然后这个变量会自动反映用户选择的值。
如果您可能需要指定选项,那么您还需要提供一个 value 属性并监听 @change。
您需要将选项绑定到类别。即 v-bind:options
或简写 :options
我还建议您使用一种方法来进行 ajax 调用,然后改为调用 mounted()
中的方法。另外,您可能想要 select 上的 v-model
,所以我在示例中添加了它。
首先在您的模板中...
<v-select v-model="selectedCategory" label="category_desc" :options="category"></v-select>
然后在你的脚本定义中...
data(){
return{
category:[],
selectedCategory: null,
}
},
methods:{
getCategories(){
this.$http.get('http://localhost:3000/api/categories')
.then(function (response) {
this.category=response.data
}.bind(this))
.catch(function (error) {
console.log("error.response");
});
}
},
mounted(){
this.getCategories();
}
这是一个有效的 jfiddle https://jsfiddle.net/skribe/mghbLyva/3/
我是 vue(2.5 版)的新手,在我的项目中安装了 v-select 我浏览了文档,但对一些事情感到困惑 这是我的代码
<template>
<div class="wrapper">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="name">Category</label>
<v-select label="category_desc" options="category"></v-select>
</div>
</div>
</div>
<button type="button" variant="primary" class="px-4" @click.prevent="next()">next</button>
</div>
</template>
<script>
export default {
name: 'Addproduct',
data () {
return {
category:[]
}
},
mounted() {
this.$http.get('http://localhost:3000/api/categories') .then(function (response) {
console.log(response.data);
this.category=response.data
})
.catch(function (error) {
console.log("error.response");
});
},
方法:{ 下一个(){ // console.log('value of v-selection not option' ) 例如 id 1 有 'country' 所以我想在方法 next() 中使用 id 1 即在 console.log } } 现在我的问题是我如何将这个 axios 响应的成功值传递给 v-select 选项,其次我如何获得 v-select 的 selected 值例如;- 当用户单击下一步按钮我如何获得在 v-select
中 selected 的值我们是在谈论 this select component 吗?最简单的用法是在 v-select 上放置一个 v-model 属性。然后这个变量会自动反映用户选择的值。
如果您可能需要指定选项,那么您还需要提供一个 value 属性并监听 @change。
您需要将选项绑定到类别。即 v-bind:options
或简写 :options
我还建议您使用一种方法来进行 ajax 调用,然后改为调用 mounted()
中的方法。另外,您可能想要 select 上的 v-model
,所以我在示例中添加了它。
首先在您的模板中...
<v-select v-model="selectedCategory" label="category_desc" :options="category"></v-select>
然后在你的脚本定义中...
data(){
return{
category:[],
selectedCategory: null,
}
},
methods:{
getCategories(){
this.$http.get('http://localhost:3000/api/categories')
.then(function (response) {
this.category=response.data
}.bind(this))
.catch(function (error) {
console.log("error.response");
});
}
},
mounted(){
this.getCategories();
}
这是一个有效的 jfiddle https://jsfiddle.net/skribe/mghbLyva/3/