• Vue--1.7watch侦听器(监视器)


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

    语法:

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

    1. const app = new Vue({
    2. el: '#app',
    3. data: {
    4. words:''
    5. },
    6. watch:{
    7. words(newValue,oldValue){
    8. }
    9. }
    10. })
    1. const app = new Vue({
    2. el: '#app',
    3. data: {
    4. obj: {
    5. words: ''
    6. }
    7. },
    8. watch: {
    9. 'obj.words'(newValue) {
    10. }
    11. }
    12. })

    案例:翻译软件

    1. <!doctype html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. </head>
    6. <body>
    7. <div id="app">
    8. <textarea rows="10" cols="40" v-model="obj.words"></textarea>
    9. <textarea rows="10" cols="40" v-model="result"></textarea>
    10. </div>
    11. <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    12. <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
    13. <script>
    14. const app = new Vue({
    15. el: '#app',
    16. data: {
    17. obj: {
    18. words: ''
    19. },
    20. result: '',
    21. timer: null
    22. },
    23. watch: {
    24. 'obj.words'(newValue) {
    25. clearTimeout(this.timer)//防抖
    26. this.timer = setTimeout(async () => {
    27. const res = await axios({
    28. url: 'https://applet-base-api-t.itheima.net/api/translate',
    29. params: {
    30. words: newValue
    31. }
    32. })
    33. this.result = res.data.data
    34. }, 300)
    35. }
    36. }
    37. })
    38. </script>
    39. </body>
    40. </html>

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

    1)deep:true对复杂类型深度监视

    2)immediate:true初始化立刻执行一次handler方法

    1. const app = new Vue({
    2. el: '#app',
    3. data: {
    4. obj: {
    5. words: '',
    6. lang:''
    7. }
    8. },
    9. watch: {
    10. 数据属性名:{
    11. deep:true,//深度监视
    12. immediate: true,//立即执行,一进入页面handler就立刻执行
    13. handler(newValue){
    14. }
    15. }
    16. }
    17. })

    案例:翻译软件Pro

    1. <!doctype html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. </head>
    6. <body>
    7. <div id="app">
    8. 翻译成语言:<select v-model="obj.lang">
    9. <option value="english">英语</option>
    10. <option value="franch">法语</option>
    11. <option value="italy">意大利</option>
    12. </select>
    13. <br><br>
    14. <textarea rows="10" cols="40" v-model="obj.words"></textarea>
    15. <textarea rows="10" cols="40" v-model="result"></textarea>
    16. </div>
    17. <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    18. <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
    19. <script>
    20. const app = new Vue({
    21. el: '#app',
    22. data: {
    23. obj: {
    24. words: '',
    25. lang: 'english'
    26. },
    27. result: '',
    28. timer: null
    29. },
    30. watch: {
    31. obj: {
    32. deep: true,
    33. immediate: true,
    34. handler(newValue) {
    35. clearTimeout(this.timer)//防抖
    36. this.timer = setTimeout(async () => {
    37. const res = await axios({
    38. url: 'https://applet-base-api-t.itheima.net/api/translate',
    39. params: newValue
    40. })
    41. this.result = res.data.data
    42. }, 300)
    43. }
    44. }
    45. }
    46. })
    47. </script>
    48. </body>
    49. </html>

    监听Vuex中的数据(.vue文件)

    1. watch: {
    2. "$store.state.xxx": {
    3. handler: function (newV) {
    4. console.log("watch中:", newV);
    5. },
    6. },
    7. }
  • 相关阅读:
    CentOS8 安装 erlang 和 RabbitMQ
    杨校老师项目之SpringBoot整合Vue与微信小程序的外卖订餐系统
    Shell sed编辑器
    SpringCloud解决feign调用token丢失问题
    【附源码】Python计算机毕业设计企业员工信息管理
    GBase 8c V3.0.0数据类型——范围操作符
    Promise, async, await 学习
    光影交织:汽车穿越隧道的视觉盛宴
    后台管理系统加水印(react)
    docker——入门介绍、组件介绍、安装与启动、镜像相关命令、容器相关命令、应用部署
  • 原文地址:https://blog.csdn.net/weixin_46479909/article/details/133695269