lodash
是一个一致性、模块化、高性能的 JavaScript 实用工具库。
在vue官方文档中使用了lodash中的debounce函数对操作频率做限制。其引入的方式是直接引入了js
<script src="https://cdn.jsdelivr.net/npm/lodash@4.13.1/lodash.min.js"></script>而现在我们使用vue-cli脚手架搭建的项目在这样使用,明显会很不合适。所以我们需要通过npm来安装
$ npm i --save lodash然后在项目中通过require去引用
// Load the full build.var _ = require('lodash');// Load the core build.var _ = require('lodash/core');// Load the FP build for immutable auto-curried iteratee-first data-last methods.var fp = require('lodash/fp'); // Load method categories.var array = require('lodash/array');var object = require('lodash/fp/object'); // Cherry-pick methods for smaller browserify/rollup/webpack bundles.var at = require('lodash/at');var curryN = require('lodash/fp/curryN');引用在当前组件即可,如在App.vue中引用
<script>let lodash = require('lodash')export default { data () { return { firstName: 'Foo', lastName: 'Bar2', question: '', answer: 'ask me' } },methods: { getAnswer: lodash.debounce(function () { if (this.question.indexOf('?') === -1) { this.answer = 'without mark' return } this.answer = 'Thinking...' var vm = this vm.$http.get('https://yesno.wtf/api').then(function (response) { vm.answer = lodash.capitalize(response.data.answer) }) .catch(function (error) { vm.answer = 'error' + error }) }, 500)}详细的操作和文档可以去看官方的中文文档lodash
以上这篇在vue-cli中引入lodash.js并使用详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。