自定义组件v-model传值方法

慈云数据 2024-03-12 技术支持 120 0

看下方注释,顺序为1=>10

自定义组件v-model传值方法
(图片来源网络,侵删)
    
    
    
        
            {{ item.name }}
            {{ item.id }}
        
        
            
        
    


import { values } from 'min-dash'
import { listStudents, submitOrder } from '@/api/acd/students'
export default {
    name: "search",
    props: {
        placeholder: {
            type: string,
            default: "请输入值"
        },
        // 定义内部用于双向绑定的变量,中间值
        value: {
            type:  [Number,String],
            default: ""
        },
        searchMethod: {
            type: Function,
            default: () => { }
        }
    },
    created() {
        // 4.在调用子组件之前,将父组件传入的v-model的值value赋值给innerValue
        this.innerValue = this.value
    },
    watch: {
        // 3.监听父组件传入的v-model的值value的变化,将起变化后value的值实时赋值给innerValue
        value: {
            handler(val) {
                this.innerValue = val
            }
        }
    },
    data() {
        return {
            // 2. 双向绑定v-model
            innerValue: null,
            test: "测试",
            list: [],
            options: [],
            // value: '',
            loading: false,
            studentList: [],
            // placeholder: "请"
        }
    },
    methods: {
        // 5.最后,将子组件下拉框中选中的值实时赋值给父组件传入的v-model的值value
        changeValue(val) {
            this.$emit('input', val);
        },
        searchStudent(s) {
            if (s) {
                this.loading = true;
                clearTimeout(this.timer); // 清除之前的定时器
                this.timer = setTimeout(() => {
                    // 在延迟后执行你的操作
                    // 调用searchMethod方法,并传入参数s
                    // 这里不要忘记加this,否则this指向会出问题
                    this.searchMethod({ name: s }).then(response => {
                        this.loading = false;
                        this.studentList = response.rows;
                    })
                }, 500); // 设置延迟时间,单位为毫秒
            }
        },
    }
}

自定义组件v-model传值方法
(图片来源网络,侵删)
微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon