vue中防抖函数,element select组件

❤️砥砺前行,不负余光,永远在路上❤️

前言

节流: n 秒内只运行一次,若在 n 秒内重复触发,只有一次生效
防抖: n 秒后在执行该事件,若在 n 秒内被重复触发,则重新计时

一、vue中防抖函数

使用场景:这里下拉框多选之后,三秒获取后边条件的列表数据(根据前边选择的内容获取新数据),直接使用@change 的话选一个就会请求数据。

在这里插入图片描述

//防抖函数(select改变)
debounce(func, wait = 3000, immediate = false) {
  // 清除定时器
  if (this.timeout !== null) clearTimeout(this.timeout);
  // 立即执行,此类情况一般用不到
  if (immediate) {
    var callNow = !this.timeout;
    this.timeout = setTimeout(function () {
      this.timeout = null;
    }, wait);
    if (callNow) typeof func === "function" && func();
  } else {
    // 设置定时器,当最后一次操作后,timeout不会再被清除,所以在延时wait毫秒后执行func回调方法
    this.timeout = setTimeout(function () {
      typeof func === "function" && func();
    }, wait);
  }
},

注意:data中需要一个timeout别忘了。

使用

changeSeList(val) {  
  let fn = async () => {
    this.getSeList(); //这个方法是获取数据的  
  };
  this.debounce(fn, 3000); //注意 这里直接传入this.getSeList() 不起作用
},

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 答:Vue3 可以使用 element-plus 的 select 组件获取 lable,可以通过以下代码实现:<el-select v-model="value" placeholder="请选择"> <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option> </el-select> ### 回答2: 在Vue 3,获取Element Plus Select组件的label,可以通过ref引用和watch监听来实现。首先,需要在Vue组件的data声明一个ref引用,用来存储Select组件的实例。然后,在mounted生命周期钩子函数,使用`this.$refs`获取Select组件的实例,并将其赋值给之前声明的ref引用。接下来,可以在watch监听ref引用的变化,在变化时获取Select组件的label。 具体实现步骤如下: 1. 在Vue组件的data声明一个ref引用,用来存储Select组件的实例: ```javascript data() { return { selectRef: null }; } ``` 2. 在mounted生命周期钩子函数,使用`this.$refs`获取Select组件的实例,并将其赋值给之前声明的ref引用: ```javascript mounted() { this.selectRef = this.$refs.select; } ``` 3. 使用watch监听ref引用的变化,在变化时获取Select组件的label: ```javascript watch: { selectRef(newVal) { if (newVal) { const label = newVal.label; console.log(label); // 在控制台打印label } } } ``` 总结:通过使用ref引用和watch监听,可以在Vue 3获取Element Plus Select组件的label。首先,声明一个ref引用来存储Select组件的实例,在mounted生命周期钩子函数获取实例并赋值给ref引用,然后使用watch监听ref引用的变化,并在变化时获取Select组件的label。 ### 回答3: 要获取 Element Plus Select 组件的 label,可以使用 ref 来引用该组件,并通过 $el 属性访问其 DOM 元素,再通过 querySelector 方法找到 label 元素,最后获取其文本内容。 首先,我们需要在 Vue 组件的 setup 函数定义一个 ref 引用: ```javascript import { ref } from 'vue'; export default { setup() { const selectRef = ref(null); // 创建 ref 引用 //... return { selectRef }; } } ``` 然后,在模板使用 ref 引用将 Select 组件与 ref 建立关联: ```html <template> <el-select v-model="selectedValue" ref="selectRef"> <!-- 选项项 --> </el-select> </template> ``` 接下来,在需要获取 label 的地方,可以通过 ref 的 value 属性获取组件的引用,并通过 $el 属性访问其 DOM 元素,使用 querySelector 方法找到 label 元素,最后获取其文本内容: ```javascript import { onMounted } from 'vue'; export default { setup() { //... onMounted(() => { const selectElement = selectRef.value.$el; // 获取 Select 组件的 DOM 元素 const labelElement = selectElement.querySelector('.el-select__label'); // 通过类名找到 label 元素 const label = labelElement.innerText; // 获取 label 元素的文本内容 console.log(label); // 输出 label 的文本内容 }); return { selectRef }; } } ``` 通过以上步骤,我们可以在 Vue 3 的组件成功获取到 Element Plus Select 组件的 label 值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

codernmx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值