//自定義組件使用 v-model
因此,對於一個帶有 v-model 的組件,它應該如下:
1. 接收一個 value prop
2.觸發 input 事件,並傳入新值
// user.vue組件
<template>
<select v-model="text">
<option :value="user.id" v-for="user in userList">
{{user.name}}
</option>
</select>
</template>
<script>
export default {
props: ['value'], // 接收一個 value prop
computed: {
userList() {
//獲取數據
},
text: {
get: function() {
return this.value;
},
set: function(val) {
this.$emit("input", val);// 利用 $emit 觸發 input 事件
}
},
},
};
</script>