vue使用lodash

2020-01-04 08:05:05

推荐第二种,方便,好用  参考地址 vue如何引入lodash

一、安装

cnpm i lodash -S

二、方法一

1、引入

import _ from 'lodash'Vue.prototype._ = _

2、使用

this._.debounce(this.handleClick,1000,false)

二、方法二

1、引入

let _ = require('lodash')

2、使用

_.debounce(this.handleClick,1000,false)

三、vue单文件组件中使用

里面分别有我自己写的debounce函数和lodash的debounce函数,效果一样!

<template>
  <div>
    <el-button @click="myDebounce">我的debounce</el-button>
    <el-button @click="_debounce">lodash的debounce</el-button>
  </div></template><script>import { debounce } from '@/utils/util'
let _ = require('lodash')
export default {
  methods: {
    handleClick1() {
      console.log(`真正执行的函数,次数比较少:handleClick1.....`)
    },
    handleClick2() {
      console.log(`真正执行的函数,次数比较少:handleClick2.....`)
    },
    myDebounce() {
      console.log(`myDebounce.....`)
      this.DB()
    },
    _debounce() {
      console.log(`_debounce.....`)
       this._DB()
    }
  },
  created() {
    this.DB = debounce(this.handleClick1, 1000, false)
    this._DB = this._.debounce(this.handleClick2,1000,false)
  }
}</script>


debounce测试.png


注意:以前我是在data选项里面定义DB:null,然后再methods里面初始化函数,但是需要判断‘如果有了就不赋函数,如果为空就赋’,发现比较麻烦;后来直接在created钩子里面定义,就很方便了!




  • 2020-12-06 16:46:11

    git撤销pull

    刚刚不小心pull了一下,有错误,想撤回怎么办。

  • 2020-12-06 19:05:13

    visual studio 配置 intellij idea快捷键

    我原本从intellij idea转换到visual studio是因为webstorm没办法远程开发,而visual studio有remote wsl,和remote ssh,看着挺不错的样子。

  • 2020-12-06 20:38:30

    intellij idea远程开发remote

    开发时一般的平台都是windows,但windows对开发极其不友好,一般都会在本地开启虚拟机,安装上linux环境进行项目的部署测试。下面介绍一种windows主机与linux虚拟机代码同步的方法。这个工具适用于jerbrains公司旗下的很多产品,比如idea、webstrom、phpstrom等。但是要注意你安装的IDE必须是专业版的,社区版的IDE是没有这个代码同步功能的哦!

  • 2020-12-07 05:46:56

    npm设置和取消代理的方法

    有时候是设置了全局代理对npm并不生效,不如直接给npm设置代理,至少在mac电脑我是有这种感觉的。