uView的DatetimePicker组件在confirm回调中取不到v-model的最新值
前情
uni-app是我比较喜欢的跨平台框架,它能开发小程序/H5/APP(安卓/iOS),重要的是对前端开发友好,自带的IDE让开发体验非常棒,公司项目就是主推uni-app,在uniapp生态中uView是其中非常好的全平台的第三方开源ui库,我在公司项目中果断的使用了它。
我一个过滤弹窗有二个表单需要选择开始日期和结束日期,于是想到复用一个日期选择组件,通过设置不同标志来区分是不同表单触发的,再在confrim回调里根据不同标志把v-model上的值赋值到不同表单上
示例代码片段:
<template>
<view class="page-container">
<custom-popup
:isVisble="filterFormVisible"
custom-style="top:206rpx;"
@maskClick="fliterFormToggle"
>
<view class="filter-main">
<view class="filter-content">
// ......
<text class="filter-type">入驻时间:</text>
<view class="filter-item-date-wrap">
<u-input
v-model="filterDateStart"
placeholder="选择开始时间"
readonly
@click.native="pickerDate('filterDateStart')"
>
<text slot="prefix" class="iconfont"></text>
</u-input>
<view class="filter-connect"></view>
<u-input
v-model="filterDateEnd"
placeholder="选择结束时间"
readonly
@click.native="pickerDate('filterDateEnd')"
>
<text slot="prefix" class="iconfont"></text>
</u-input>
</view>
</view>
// ......
</view>
</custom-popup>
// 日期选择器
<u-datetime-picker
:show="showDatePicker"
ref="datetimePicker"
mode="date"
v-model="filterDate"
:formatter="formatter"
@cancel="showDatePicker = false"
@confirm="confirmFilterDate"
/>
</view>
</template>
<script>
import customPopup from '../../components/customPopup/customPopup.vue';
export default {
components: {
customPopup
},
data() {
return {
filterFormVisible: false,
showDatePicker: false,
filterDateStart: '',
filterDateEnd: '',
filterDate: new Date().getTime()
}
},
mounted() {
// 微信小程序需要用此写法
this.$refs.datetimePicker.setFormatter(this.formatter)
},
methods: {
filterDateChange(e) {
console.log('---- filterDateChange ----:', e);
this.filterForm.regDate = e.detail.value;
},
filterReset() {
},
filterSubmit() {
},
fliterFormToggle() {
this.filterFormVisible = !this.filterFormVisible;
},
pickerDate(type) {
this.showDatePicker = true;
// 标志变量,用于区分是哪个表单触发的
this.dateType = type;
},
confirmFilterDate() {
console.log('---- confirmFilterDate ----:', this.filterDate);
// 通过标志变量,把值赋值到正确的表单上
this[this.dateType] = e.value;
this.showDatePicker = false;
},
formatter(type, value) {
if (type === 'year') {
return `${value}年`
}
if (type === 'month') {
return `${value}月`
}
if (type === 'day') {
return `${value}日`
}
return value
},
}
}
</script>
// ......
坑位
但是事与愿违,在confirm回调里取到v-model绑定的值不是最新的,而是上一次的值,更奇怪的是你点击二次就能拿到最新的值了
Why?
通过看了DatetimePicker的源码发现,引起这个问题的原因很简单,因为它的代码是先触发confirm回调,再才触发input事件去更新v-model上的值,当你在confirm回调去取值的时候,值还没有更新,点第二次可以那是因为值已经更新了。
组件源码片段如下:
......
// 点击工具栏的确定按钮
confirm() {
this.$emit('confirm', {
value: this.innerValue,
mode: this.mode
})
this.$emit('input', this.innerValue)
},
......
解决方案
方案1
既然发现是因为更新延后,我们可以加个定时器来获取
示例代码片段:
......
confirmFilterDate() {
console.log('---- confirmFilterDate ----:0', this.filterDate);
setTimeout(() => {
console.log('---- confirmFilterDate ----:1', this.filterDate);
// 通过标志变量,把值赋值到正确的表单上
this[this.dateType] = this.filterDate;
this.showDatePicker = false;
}, 0);
},
......
方案2
通过vue自带的$nextTick来解决
示例代码片段:
......
confirmFilterDate() {
console.log('---- confirmFilterDate ----:0', this.filterDate);
this.$nextTick(() => {
console.log('---- confirmFilterDate ----:1', this.filterDate);
this[this.dateType] = this.filterDate;
this.showDatePicker = false;
})
},
......
方案3(目前推荐)
其实可以不用依赖v-model值,confirm回调有把最新的日期选择值传回来,直接取那个值即可
示例代码片段:
confirmFilterDate(e) {
console.log('---- confirmFilterDate ----:0', this.filterDate, e);
this[this.dateType] = e.value;
this.showDatePicker = false;
},
或者可以再等等,我有向uView官方提了一个PR:https://github.com/umicro/uView2.0/pull/1068 希望能优化这一个问题,如果能合并那后续就可以在confirm回调里愉快的获取v-model最新值了。
uView的DatetimePicker组件在confirm回调中取不到v-model的最新值的更多相关文章
- Thymeleaf模板引擎与springboot关联后,在html中无法使用el表达式获取model存的值
头部引入了thymeleaf <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thy ...
- uView的DatetimePicker详解
uView UI号称: 是全面兼容nvue的uni-app生态框架,全面的组件和便捷的工具会让您信手拈来,如鱼得水 亲身感受,用起来真的坑太多, 官方文档太简洁, 很多配置都没说明也没代码, 上百度查 ...
- Android异步回调中的UI同步性问题
Android程序编码过程中,回调无处不在.从最常见的Activity生命周期回调开始,到BroadcastReceiver.Service以及Sqlite等.Activity.BroadcastRe ...
- React onBlur回调中使用document.activeElement返回body解决方案
最开始想实现一个功能,点击img图标后给出购物下拉框CartDropdown,当img及CartDropdown失去焦点时隐藏CartDropdown. 最开始的核心代码如下: export defa ...
- ajax回调中的this.href不执行跳转的解决办法
1. 问题背景 如下所示代码: $.post("/ems/register",indata, function(data){ if(data != null && ...
- jQuery:在一个回调中处理多个请求
我曾经为Mozilla Developer Network 开发一个新功能,它需要加载一个基本的脚本文件的同时加载一个JSON请求.因为我们使用的是jQuery,意味着要使用 jQuery.getSc ...
- 使用domain模块捕获异步回调中的异常
和其他服务器端语言相比,貌似node.js 对于异常捕捉确实非常困难. 首先你会想到try/catch ,但是在使用过程中我们会发现并没有真正将错误控制在try/catch 语句中. 为什么? 答案是 ...
- Maya cmds pymel scriptJob() 和 undoInfo() 在回调中撤销(undo)
Maya cmds pymel scriptJob() 和 undoInfo() 在回调中撤销(undo) def myEventFun(): cmds.undoInfo(stateWithoutFl ...
- model,map,MapAndVivew用于页面跳转时候使用的即跳转后才添加属性 这样再回调中无法使用 因为回调的前提是页面不调转;解决的方法是用responsewrite(普通的字符响应)
model,map,MapAndVivew用于页面跳转时候使用的即跳转后才添加属性 这样再回调中无法使用 因为回调的前提是页面不调转:解决的方法是用responsewrite
- 使用Unity中的Box Collider组件完成游戏场景中的碰撞检测功能
一.介绍 目的:通过Unity自带的组件完成游戏场景中的碰撞检测功能. 软件环境:Unity 2017.3.0f3 二.实现过程 1,在面板中点击Add Component按钮 2,添加Box Col ...
随机推荐
- 开源项目dotnet/eshop 和 dotnet/eshopsupport
dotnet/eshop[1] 和 dotnet/eshopsupport[2] 是两个与 .NET 相关的开源项目,分别用于展示电子商务应用的不同方面. dotnet/eshop: 功能与架构:do ...
- 教你一招,测试人员如何通过AI提高工作效率!
伴随着AI技术的兴起,像OpenAI推出的ChatGPT.Microsoft发布的Microsoft 365 Copilot.阿里的通义千问.百度的文心一言.华为的盘古大模型等.很多测试人员开始担心, ...
- idea运行java项目main方法报build failure错误的解决方法
当在使用 IntelliJ IDEA 运行 Java 项目的 main 方法时遇到 "Build Failure" 错误,这通常意味着在项目的构建过程中遇到了问题.解决这类问题通常 ...
- Java在线数据导入导出Excel
利用hutool 导入导出 Excel 导入maven <dependency> <groupId>cn.hutool</groupId> <artifact ...
- 图片 电力电网行业IT运维方案
智能电网背景下,电力.电网企业信息化逐渐渗透到其业务链的各个环节,云计算.物联网.移动互联网等新技术的应用,更驱动信息化与业务创新深度融合.电力.电网企业集团信息系统群逐渐朝着一体化方向发展,信息链越 ...
- 03-jsx中使用js表达式
// 在jsx中使用 js 表达式 /// 通过一个 {} 展示变量即可 vue 中使用 {{}} 展示js表达式 // 什么是js表达式 有结果的 import reactDom from &quo ...
- 一、Spring Boot集成Spring Security专栏
一.Spring Boot集成Spring Security专栏 一.Spring Boot集成Spring Security之自动装配 二.实现功能及软件版本说明 使用Spring Boot集成Sp ...
- Linux+PXE+DHCP+TFTP+NFS实现无人值守安装
一.实验环境:OS:Redhat6.4软件:DHCP服务.TFTP服务.NFS服务硬件:Dell R630服务器两台物理连接图: em1 em1 二.工作原理:服务器通过PXE网卡启动,从dhcp服务 ...
- KubeSphere 社区双周报 | 本周六上海站 Meetup 准时开启 | 2023.7.21-08.03
KubeSphere 社区双周报主要整理展示新增的贡献者名单和证书.新增的讲师证书以及两周内提交过 commit 的贡献者,并对近期重要的 PR 进行解析,同时还包含了线上/线下活动和布道推广等一系列 ...
- 黑客工具:Amass – 寻找子域
安装 所有信息都可以在 OWASP Amass 项目的 Github 页面上找到:https://github.com/OWASP/Amass.我们将一起完成安装过程,以便更快地部署. 转到发布页面并 ...