Watch监听器和防抖功能

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8" />

<meta http-equiv="X-UA-Compatible" content="IE=edge" />

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>watch侦听器</title>

<!-- watch侦听器

作用:监视数据变化,执行一些 业务逻辑或异步操作

语法:

1.简单写法 简单类型数据,直接监视

2.完整写法 添加额外的配置项 -->

<style>

* {

margin: 0;

padding: 0;

box-sizing: border-box;

font-size: 18px;

}

#app {

padding: 10px 20px;

}

.query {

margin: 10px 0;

}

.box {

display: flex;

}

textarea {

width: 300px;

height: 160px;

font-size: 18px;

border: 1px solid #dedede;

outline: none;

resize: none;

padding: 10px;

}

textarea:hover {

border: 1px solid #1589f5;

}

.transbox {

width: 300px;

height: 160px;

background-color: #f0f0f0;

padding: 10px;

border: none;

}

.tip-box {

width: 300px;

height: 25px;

line-height: 25px;

display: flex;

}

.tip-box span {

flex: 1;

text-align: center;

}

.query span {

font-size: 18px;

}

.input-wrap {

position: relative;

}

.input-wrap span {

position: absolute;

right: 15px;

bottom: 15px;

font-size: 12px;

}

.input-wrap i {

font-size: 20px;

font-style: normal;

}

</style>

</head>

<body>

<div id="app">

<!-- 条件选择框 -->

<div class="query">

<span>翻译成的语言:</span>

<select>

<option value="italy">意大利</option>

<option value="english">英语</option>

<option value="german">德语</option>

</select>

</div>

<!-- 翻译框 -->

<div class="box">

<div class="input-wrap">

<textarea v-model="obj.words"></textarea>

<span><i>⌨️</i>文档翻译</span>

</div>

<div class="output-wrap">

<div class="transbox">{{ result }}</div>

</div>

</div>

</div>

<script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>

<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

<script>

// 接口地址:https://applet-base-api-t.itheima.net/api/translate

// 请求方式:get

// 请求参数:

// (1)words:需要被翻译的文本(必传)

// (2)lang: 需要被翻译成的语言(可选)默认值-意大利

// -----------------------------------------------

const app = new Vue({

el: '#app',

data: {

// words: ''

obj: {

words: ''

},

result: '', // 翻译结果

timer: null // 延时器id

},

// 具体讲解:(1) watch语法 (2) 具体业务实现

watch: {

// 侦听器

// 侦听谁就写谁

// 该方法会在数据变化时调用执行

// newValue新值,oldValue老值(一般不用)

// 该方法会在数据变化时调用执行

// newValue新值, oldValue老值(一般不用)

// words (newValue) {

// console.log('变化了', newValue)

// }

// 拿结果

// async 'obj.words'(newValue){

// // 发请求

// const res = await axios({

// url:'https://applet-base-api-t.itheima.net/api/translate'

// // 接口

// params:{

// words:newValue

// }

// })

// }

'obj.words' (newValue) {

// 此时就可以监视到obj里面的words

// console.log('变化了', newValue)

// 防抖: 延迟执行 → 干啥事先等一等,延迟一会,一段时间内没有再次触发,才执行

clearTimeout(this.timer)

this.timer = setTimeout(async () => {

//

const res = await axios({

url: 'https://applet-base-api-t.itheima.net/api/translate',

params: {

words: newValue

}

})

this.result = res.data.data

console.log(res.data.data)

}, 300)

}

}

})

</script>

</body>

</html>

相关推荐
开开心心就好1 小时前
批量提取PDF中的图片,直接导出原图
前端·javascript·支持向量机·智能手机·pdf·html·启发式算法
Setsuna_F_Seiei4 小时前
前端转型 Agent 开发 05 之 Agent Hooks 与 Checkpointer(让 Agent 从全自动转变人为可掌控)
前端·agent·ai编程
百万蹄蹄向前冲5 小时前
风扇转了一晚上MVP专家团翻车事故
前端·人工智能
默_笙6 小时前
🏛 给 AI 配一间办公室:Harness Engineering 六大模块与它的实现
前端·javascript
linux_cfan7 小时前
videojs v10 源代码系列解读:14 · 谓词守卫:在运行时安全地调用能力
前端·javascript·音视频
kyriewen7 小时前
我扒了 10,221 条 JD:腾讯技术岗 75% 在要 AI
前端·人工智能·ai编程
郑州光合科技余经理8 小时前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
+VX:Fegn08958 小时前
计算机毕业设计|基于springboot + vue旅游管理系统(源码+数据库+文档)
java·开发语言·vue.js·spring boot·课程设计
雪芽蓝域zzs9 小时前
第 7 章:双 Y 轴组合图(柱状 + 折线,看板高频场景)
vue.js·echars
IT_陈寒9 小时前
Vue的响应式让我熬到凌晨三点,原来漏了这个小细节
前端·人工智能·后端