P粉5123632332023-08-21 18:53:56
v-model should be given a data variable, not a function. In Vue 3, you should also declare this variable using ref
or reactive
to create reactive state, for example:
const filter = ref('')
Now, filter
will save the value of the selected option when set to the selector's v-model. Then, the second thing you need to do is use an "on change" event listener to respond to changes in the selection, so that every time the filter
is updated, you can commit it to your vuex store.
<template> <div class="filter"> <select v-model="filter" @change="filterChange"> <option value="">全部</option> <option value="Alive">存活</option> <option value="Dead">死亡</option> <option value="unknown">未知</option> </select> </div> </template>
<script> import { ref } from "vue"; export default { setup() { const filter = ref(""); const filterChange = (e) => { console.log("filter", filter.value); // 可选:从实际事件中获取值,而不是使用v-model console.log("来自事件的filter", e.target.value); }; return { filter, filterChange, }; }, }; </script>