ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

[javascript] js判断对象类型typeof与instanceof解决elementui时间插件默认时间问题

2021-06-17 19:02:53  阅读:136  来源: 互联网

标签:instanceof 插件 elementui res Date result pickTime data


在页面上有一个时间插件 , 默认是没有绑定初始时间的 , 当需要绑定初始时候时 , 只能给它赋值当前日期的Date对象

但是在调用接口的时候 , 要求传递的是一个指定格式的字符串 , 需要把Date对象转成时间字符串 , 这个时候就需要判断类型了

 

typeof 一般只能返回如下几个结果:"number"、"string"、"boolean"、"object"、"function" 和 "undefined"。无法指定是Date类型 , 因此这里需要使用instanceof

用法是console.log(xxx instanceof Date)  这个语句会返回true或者false ,来判断对象类型

 

在elementui下给时间默认值这样用 , html部分

<el-date-picker
                                                    v-model="pickTime"
                                                    align="right"
                                                    type="date"
                                                    placeholder="选择日期"
                                                    value-format="yyyyMMdd"
                                                    :picker-options="pickerOptions">
                                            </el-date-picker>

绑定的pickTime , 在data里给一个初始值 pickTime:new Date(),

在进行搜索查询函数里进行类型判断 , 并且转换一下 , 格式转换函数在上一篇文章有

                getKeywordsList: function () {
                    let _this = this;
                    let data={};
                    if(this.keywordSearchKey!=""){
                        data.word=this.keywordSearchKey;
                    }
                    if(this.pickTime instanceof Date){
                        data.dt=this.formateTime(this.pickTime);
                    }
                    if(this.pickTime != "" && !(this.pickTime instanceof Date)){
                        data.dt=this.pickTime;
                    }
                    $.get('index.php?r=media/getkeywordsdata',data, function (rs) {
                        let res= JSON.parse(rs);
                        _this.keywordsList=res.result.rows;
                        _this.keywordCount=res.result.total;
                        _this.keywordSearchCount=res.result.total;
                        _this.displayAll();
                        _this.fullscreenLoading=false;
                    })
                },

 

 

标签:instanceof,插件,elementui,res,Date,result,pickTime,data
来源: https://blog.51cto.com/u_15274085/2918201

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有