zoukankan      html  css  js  c++  java
  • Vue watch 监听手机号输入完毕后请求数据

    Bac: 需要在用户输入手机号之后,调用后台接口去获取对应该手机号的数据情况

    思路是:监听用户输入手机号,当输入的手机号长度 = 11 的时候,此时再去请求接口。

    在这里监听学习到了两种方式:

    一、watch 监听 计算属性的值

    watch:{
              customerMobile (val, oldval) {
                if (val.length === 11) {
                  this.handleCustomerChange(val)
                }
                // clearTimeout(this.timeout)
                //   this.timeout = setTimeout(() => {
                //     this.handleCustomerChange(val)
                //   }, 500)
              }
            },
     computed : {
              customerMobile () {
                return this.addForm.mobile
              }
        },
      methods: {
              // 输入手机号,客户名称联系人联动
              handleCustomerChange(){
                this.$http.get(`${window.SITE_CONFIG['baseURL']}/presale/presalelist/getDataByMobile`,
                {
                  params:{
                    mobile: this.addForm.mobile
                  }
                })
                .then(data => {
                  if (data.result.code === 0){
                    this.addForm.username = data.result.data.customerName
                    this.addForm.linkman = data.result.data.name
                  } else {
                    this.$message({
                      message: data.result.msg,
                      type: 'error',
                      duration: 1500
                    })
                  }
                })
              }
    }
     

    二、watch直接监听某个对象的属性

    watch:{
              'addForm.mobile' : function(val, oldval){
                  if (val.length === 11) {
                  this.handleCustomerChange(val)
                }
              }
           },
  • 相关阅读:
    学算法的那些年,吴师兄接触的网站、软件、视频、书籍大揭秘
    阮一峰:CSS Modules 用法教程
    截取url参数
    在dotnet core实现类似crontab的定时任务
    开源一个基于dotnet standard的轻量级的ORM框架-Light.Data
    ABP Vnext使用mysql数据库
    实现ElementUI Dialog宽度响应式变化
    使用Vue Baidu Map对百度地图实现输入框搜索定位
    使用Docker搭建HttpRunnerManager环境
    SpringBoot集成spring aop开发
  • 原文地址:https://www.cnblogs.com/rabbit-lin0903/p/12665400.html
Copyright © 2011-2022 走看看