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 ...
随机推荐
- Centos7 阿里云镜像 2207-02 下安装docker-compose后,docker-compose version 命令失效问题
吐槽下,按照官方教程和网上各种教程折腾了很久,最后试出来的. 首先找到docker-compose被安装到那里 whereis docker --输出示例,cd命令进入各自目录查看docker-com ...
- 均值回归策略在A股ETF市场获利的可能性
如何在股票市场获利 曾经有人告诉我一个在股票市场赚钱的秘诀,只要掌握这个秘诀,赚钱就像捡钱一样容易.他说:这个秘诀其实很简单,就是在股票价格低的时候买入,在价格高的时候卖出. 啧啧,不愧是秘诀,明明是 ...
- Java项目笔记(二)
一.分页待解决的问题 分页是在service层实现的 在controller层和service层同时写了这句代码 PageHelper.startPage(Integer.valueOf(pageNo ...
- springmvc参数传递不给参数值默认值设置方法
@RequestMapping("hello") public voiid test001(@RequestParam(defaultValue = "11") ...
- linux中安装mysq5.7
linux中安装mysq5.7 一. 安装mysql yum install mariadb-server mariadb 二. 开启mysql service mysqld start 四. 停止m ...
- Nuxt.js 应用中的 kit:compatibility 事件钩子详解
title: Nuxt.js 应用中的 kit:compatibility 事件钩子详解 date: 2024/10/11 updated: 2024/10/11 author: cmdragon e ...
- python中字典的运算
问题: 如何查找在两个字典中相同的键.值元素? dict1 = {'a': 1, 'b': 2, 'c': 3} dict2 = {'a': 10, 'y': 11,'b': 2} dict1.key ...
- [2024领航杯] Pwn方向题解 babyheap
[2024领航杯] Pwn方向题解 babyheap 前言: 当然这个比赛我没有参加,是江苏省的一个比赛,附件是XiDP师傅在比赛结束之后发给我的,最近事情有点多,当时搁置了一天,昨天下午想起来这个事 ...
- WPF中为Popup和ToolTip使用WindowMaterial特效 win10/win11
先看效果图: 大致思路是:通过反射获取Popup内部的原生窗口句柄,然后通过前文已经实现的WindowMaterial类来应用窗口特效:对于ToolTip,为了保持其易用性,我使用了附加属性+全局样式 ...
- C#通过JS变量提取天天基金API返回的基金净值
目录 天天基金API 添加项目依赖项 请求 API 数据 获取所有基金代码 获取基金净值信息 功能测试 参考链接 天天基金API 常见的 API 如下: 所有基金代码:http://fund.east ...