需求:搜刮按钮增长防抖功能
代码1
- <template>
- <el-button type="primary" icon="el-icon-search" @click="searchClick">搜索</el-button>
- </template>
-
- <script>
- import { debounce } from "lodash";
- // 自定义搜索按钮,带防抖
- export default {
- name: "SearchButton",
- props: {
- //默认等待1秒
- wait: {
- type: Number,
- default: 1000
- }
- },
- components: {},
- data () {
- return {
- };
- },
- mounted () {
- //防抖函数
- this.debounceClick=debounce(function(){
- console.log(111111);
- },this.wait)
- },
- methods: {
- /**
- * 点击事件
- */
- searchClick () {
- this.debounceClick()
- }
- //或是
- searchClick :debounce(function(){
- console.log(111111);
- },this.wait)
- }
- };
- </script>
-
复制代码 参考:
https://vuejs.org/guide/essentials/reactivity-fundamentals.html#stateful-methods
注:debounce返回的是一个函数,debounce后面增长()或直接在点击事件中调用是不可的
- searchClick () {
- //不执行
- debounce(function(){
- console.log(111111);
- },this.wait)
- }
复制代码- searchClick () {
- //每次都执行
- debounce(function(){
- console.log(111111);
- },this.wait)()
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |