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

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

javascript 复制代码
<template>
    <!-- v-model怎么实现父子组件传值 -->
    <!-- 子组件:
        1.在子组件中,我们要在内部进行绑定v-model="innerValue" -->
    <el-select v-model="innerValue" filterable remote reserve-keyword :placeholder="placeholder" @change="changeValue"
        :remote-method="searchStudent" :loading="loading" style="width: 240px">
        <el-option v-for="item in studentList" :key="item.id" :label="item.name" :value="item.id">
            <span style="float: left">{{ item.name }}</span>
            <span style="float: right;color: var(--el-text-color-secondary);font-size: 13px;">{{ item.id }}</span>
        </el-option>
        <template #loading>
            <svg class="circular" viewBox="0 0 50 50">
                <circle class="path" cx="25" cy="25" r="20" fill="none" />
            </svg>
        </template>
    </el-select>
</template>

<script>
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); // 设置延迟时间,单位为毫秒
            }

        },
    }
}
</script>
相关推荐
我爱加班、、16 分钟前
Chrome安装最新vue-devtool插件
javascript·vue.js·chrome·vue-devtool
澄江静如练_1 小时前
小程序 存存上下滑动的页面
前端·javascript·vue.js
源码方舟1 小时前
基于SpringBoot+Vue的房屋租赁管理系统源码包(完整版)开发实战
vue.js·spring boot·后端
m0_513962531 小时前
vue-ganttastic甘特图label标签横向滚动固定方法
javascript·vue.js·甘特图
菜鸟una1 小时前
【taro3 + vue3 + webpack4】在微信小程序中的请求封装及使用
前端·vue.js·微信小程序·小程序·typescript·taro
Java&Develop1 小时前
怎么查看当前vue项目,要求的node.js版本
vue.js
松树戈2 小时前
openfeign与dubbo调用下载excel实践
vue.js·spring cloud·elementui·dubbo
码农黛兮_462 小时前
HTML、CSS 和 JavaScript 基础知识点
javascript·css·html
跑调却靠谱2 小时前
elementUI调整滚动条高度后与固定列冲突问题解决
前端·vue.js·elementui
全职计算机毕业设计3 小时前
SpringBoot Vue MySQL酒店民宿预订系统源码(支付宝沙箱支付)+代码讲解视频
vue.js·spring boot·mysql