<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>

最新文章

  1. 如何在BPM中使用REST服务(1):通过程序访问网页内容
  2. 新闻发布系统&lt;分页&gt;
  3. Redis学习笔记(一)
  4. [课程设计]Scrum 多鱼点餐系统(团队交流日)
  5. IntelliJ Idea 常用快捷键 列表(实战终极总结!!!!)
  6. Windows 8.1/Server 2012 R2/Embedded 8.1 with Update 3(MSDN最新版)
  7. 53.转:深入浅出FPGA-14-ChipScope软件使用
  8. cas的url中去掉jsessionid
  9. SQL2-子查询、join查询
  10. surface pro系统按键+重装系统
  11. 康复计划#5 Matrix-Tree定理(生成树计数)的另类证明和简单拓展
  12. 第一章:eclipse 中修改字体大小和编码格式
  13. java 关键字final
  14. cocos2d-x 欢乐捕鱼游戏总结
  15. ThreadPoolExecutor 中的 shutdown() 、awaitTermination() 、 shutdownNow() 的用法
  16. 用vue脚手架创建bootstrap-vue项目
  17. 数据结构与算法(C#)入门 --- 序
  18. Apache Tomcat Eclipse Integration
  19. npm 安装包报错 rollbackFailedOptional
  20. 20155310 2016-2017-2 《Java程序设计》第三周学习总结

热门文章

  1. 三、主流区块链技术特点及Hyperledger Fabric V1.0版本特点
  2. oracle表空间不足扩容的方法
  3. flume 1.8 安装部署
  4. [PHP] 07 - Json, XML and MySQL
  5. js的微任务和宏任务
  6. 微信小程序获取到Openid
  7. C# MVC+EF—页面搭建
  8. 关于SQL SERVER中的FLOAT转换为VARCHAR
  9. Linux之文档与目录结构 目录的相关操作 Linux的文件系统
  10. HDU 5985/nowcoder 207D - Lucky Coins - [概率题]