Vue 通过 Lodash 限制操作频率
<template>
<div id="watch-example">
<p>
Ask a yes/no question:
<input v-model="question">
</p>
<p>{{ answer }}</p>
</div>
</template> <script>
import _ from 'lodash'
import axios from 'axios' export default{
data(){
return {
question: '',
answer: 'I cannot give you an answer until you ask a question!'
}
},
watch: {
// 如果 `question` 发生改变,这个函数就会运行
question: function (newQuestion, oldQuestion) {
this.answer = 'Waiting for you to stop typing...'
this.debouncedGetAnswer()
}
},
created: function () {
// `_.debounce` 是一个通过 Lodash 限制操作频率的函数。
// 在这个例子中,我们希望限制访问 yesno.wtf/api 的频率
// AJAX 请求直到用户输入完毕才会发出。想要了解更多关于
// `_.debounce` 函数 (及其近亲 `_.throttle`) 的知识,
// 请参考:https://lodash.com/docs#debounce
this.debouncedGetAnswer = _.debounce(this.getAnswer, 500)
},
methods: {
getAnswer: function () {
if (this.question.indexOf('?') === -1) {
this.answer = 'Questions usually contain a question mark. ;-)'
return
}
this.answer = 'Thinking...'
var vm = this
axios.get('https://yesno.wtf/api')
.then(function (response) {
vm.answer = _.capitalize(response.data.answer)
})
.catch(function (error) {
vm.answer = 'Error! Could not reach the API. ' + error
})
}
}
}
</script>
Vue 通过 Lodash 限制操作频率的更多相关文章
- Vue 使用lodash库减少watch对后台请求压力
lodash需要新引入 我使用的是npm方式 使用lodash的_.debounce方法 具体代码: <!doctype html> <html lang="en" ...
- vue引入 lodash
vue main.js引入 // main.js 全局引入lodash import _ from 'lodash' Vue.prototype._ = _ // 使用 this._.debounce ...
- 10.VUE学习之使用lodash库减少watch对后台请求的压力
问题描述 使用watch监听库里word的值的变化,获取新值后,用oxios发送的ajax异步请求, 此时会多次发送请求,浪费服务器资料. 解决办法 使用lodash库里的_.debounce函数延缓 ...
- VUE 入门基础(4)
四,计算属性 基础例子 <div id='example'> <p>0riginal message: "{{message}}"</p> &l ...
- Vue.2.0.5-计算属性
计算属性 在模板中绑定表达式是非常便利的,但是它们实际上只用于简单的操作.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div id="example"> { ...
- Vue学习-01
1.vue 学习 v-bind:title 数据绑定 v-if 判断显示或者隐藏 <div id="app-3"> <p v-if="seen" ...
- Vue.js学习
<!DOCTYPE html> <html> <head> <title>xxx</title> </head> <bod ...
- 后端开发者的Vue学习之路(一)
目录 前言: iview组件库示例 element组件库示例 Vue的介绍 兼容性: 学习Vue需要的前置知识: MVVM模型 补充: 安装/导入 导入Vue 安装 两种方式的区别: HelloWor ...
- 前端学习历程--vue
---恢复内容开始--- 一.对比其他框架 1.react: 共同点: 使用 Virtual DOM 提供了响应式(Reactive)和组件化(Composable)的视图组件. 将注意力集中保持在核 ...
随机推荐
- python初级 1 数据类型和变量
一.整数(int) 例: 0 1 2 3 -1 -2 –3 In [31]: print(type(0)) <class 'int'> In [32]: print(type(1)) & ...
- metroui
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- SpringBoot Docker入门,SpringBoot Docker安装
SpringBoot Docker入门,SpringBoot Docker安装 ================================ ©Copyright 蕃薯耀 2018年4月8日 ht ...
- Android Autosizing TextViews
https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview Autosizing TextView ...
- 通过USB转TTL串口下载stm32程序
目录: 1.硬件及其接线 2.驱动及软件 3.下载程序测试 1.硬件及其接线 1.1 USB转TTL刷机板(CH340模块升级小板) 1.2 主芯片STM32F103C8T6开发板 1.3接线 1.3 ...
- [LintCode] Max Points on a Line 共线点个数
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- js 拷贝树copytree
希望能摆脱lodash的深拷贝
- Maven 项目打包需要注意到的那点事儿
1. 关于 Maven 打 war 包<使用 Eclipse 的 Maven 2 插件开发一个 JEE 项目>详细介绍了如何在 Eclipse 使用 Maven 新建一个 JEE 项目并对 ...
- Adobe Photoshop CS6简单的破解
由于网站的页面布局和素材准备等等需要用到Photoshop,所以下载了个 Photoshop CS6,写这份破解文档的大佬感觉写的很复杂,看了让人头疼,乱搞中突然发现一个方法可以很快的进行破解操作,我 ...
- MySQL 安装 用户管理 常用命令
MySQL目录 数据库概览 数据库介绍 Why Choose MySQL MySQL的前世今生 MySQL的安装 Windows安装MySQL5.721 installer版 Windows安 ...