gpt4 book ai didi

javascript - 获取 vue-select 值不起作用

转载 作者:搜寻专家 更新时间:2023-10-30 22:29:30 24 4
gpt4 key购买 nike

我正在尝试从 vue-select 中获取选定的值,但已经使用了所有方法并搜索了帮助,但这是否可行,我还在页面加载时触发了警报

Vue.component('v-select', VueSelect.VueSelect)
new Vue({
el: '#app',
data: {
options: [
{id: 1, label: 'foo'},
{id: 3, label: 'bar'},
{id: 2, label: 'baz'},
],
selected: '',
},
methods: {
runme: function() {
alert(this.selected);
}
}
})
body {
font-family: 'Source Sans Pro', 'Helvetica Neue', Arial, sans-serif;
}

h1 {
font-size: 26px;
font-weight: 600;
color: #2c3e5099;
text-rendering: optimizelegibility;
-moz-osx-font-smoothing: grayscale;
-moz-text-size-adjust: none;
}

#app {
max-width: 30em;
margin: 1em auto;
}
<script src="https://vuejs.org/js/vue.js"></script>
<script src="https://unpkg.com/vue-select@2.4.0/dist/vue-select.js"></script>
<div id="app">
<h1>Vue Select - Using v-model</h1>
<v-select v-model="selected" :on-change="runme" :options="options"></v-select>
</div>

最佳答案

vue-select 作者在这里。 on-change 回调将在 v2.5.0 中弃用,并在 v2.6.0 中移除。这是来自 v2.4.0 来源的 Prop :

/**
* An optional callback function that is called
* each time the selected value(s) change.
*
* @type {Function}
* @param {Object || String} val
*/
onChange: {
type: Function,
default: function (val) {
this.$emit('input', val)
}
}

正如 Bob Dust 所解释的,这里的关键是 onChange 调用 this.$emit('input',val),这是 Vue Hook 以提供v-model 语法。如果事件没有发出,Vue 不知道变化。

如果您需要 v-model 并且还想在值发生变化时采取行动,那么监听 @input 事件是最好的选择:

<v-select v-model="selected" @input="runme" :options="options"></v-select>

关于javascript - 获取 vue-select 值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48760384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com