1、编辑回传时间
editHandle(id) {
getNoticeById(id).then(res => {
this.$nextTick(()=>{
this.editInfo = Object.assign({},res.data) ;
if (this.editInfo.noticeType==2) {
this.editInfo.startDatetime = String(new Date(this.editInfo.startDatetime));
this.editInfo.endDatetime = String(new Date(this.editInfo.endDatetime));
}
})
this.platformAddDialog = true;
}).catch(err => {
this.$message.error(err.message || '请求公告信息失败')
})
},
2、多选提取某一项变数组
handleSelectionChange(val) {
this.multipleSelection = val.map(item => item.id) ;
},
3、删除和批量删除使用一个函数
// 批量删除
deletesHandle() {
this.delFunction(this.multipleSelection);
},
// 删除
deleteHandle(id) {
let ids = [id]
this.delFunction(ids)
},
// del Function
delFunction(ids) {
this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
delNoticeByIds(ids).then(res => {
if(res.state == 0) {
this.$message.success(res.message || '删除成功');
this.getList()
} else {
this.$message.error(res.message || '删除失败')
}
}).catch(err => {
this.$message.error(err.message || '删除失败')
})
}).catch(()=>{
this.$message.success( '取消删除')
})
this.getList()
},
4、watch多种写法
watch: {
editInfo: {
handler : function (val) {
if(val == null) {
this.resetForm('addForm');
}
this.addForm.realName = val == null ? '' : val.realName;
this.addForm.fieldLabel = val == null ? '' : val.fieldLabel;
this.addForm.industryLabel = val == null ? [] : (val.industryLabel && val.industryLabel.length > 0 ? val.industryLabel.split(',') : []);
if(this.addForm.fieldLabel && this.addForm.fieldLabel.length > 0) {
this.getIndustryList(this.addForm.fieldLabel);
}
}
},
'addForm.fieldLabel'(val) {
if(val && val.length > 0) {
this.getIndustryList(val);
}
}
}
5、标准时间 转 yyyy-MM-dd HH:mm:ss格式
A
startTime: this.YymmddFormat(this.addParamForm.startTime)
YymmddFormat(newDate) {
let Month = newDate.getMonth() + 1;
Month = Month >= 10 ? Month : '0' + Month;
let d = newDate.getDate();
d = d >= 10 ? d : '0' + d
return [
[newDate.getFullYear(), Month, d].join('-'), [newDate.getHours(), newDate.getMinutes(), newDate.getSeconds()].join(':')
].join(' ');
},
B
let date = new Date(
new Date().getFullYear(),
new Date().getMonth() - 1,
new Date().getDate()
);
let time = this.YymmddFormat(date)
YymmddFormat(newDate) {
let Month = newDate.getMonth() + 1;
Month = Month >= 10 ? Month : '0' + Month;
let d = newDate.getDate();
d = d >= 10 ? d : '0' + d
return [
[newDate.getFullYear(), Month, d].join('-')
].join(' ');
},
6、将字符串A替换成字符串B
let reg=new RegExp('<img','g')
content=content.replace(reg,'<img style="max-width:100%;height:auto;" ');
7、时间选择器禁止选择今天之后
<el-date-picker v-model="timeValue" type="date" placeholder="选择查看日期" :picker-options="pickerOptions"></el-date-picker>
...
data() {
return {
timeValue:'',
pickerOptions:{
disabledDate(time) {
return time.getTime() >= (Date.now() - (24*60*60*1000))
}
},
}
},
8、数组变了但页面不渲染(数组改变太深)
// 把数组转换成json然后再转换回来
vm.arr=JSON.parse(JSON.stringify(vm.arr));
9、一选择器改变值后二选择器自动清空
可编辑表格中有两个选择器,第一个select选择器,选择项变化时,清空第二个select选择器选择项并请求第二个选择器下拉列表数据,当做出清空选择器绑定数据后,第二个选择器下拉项无法选择。
利用this.$set
替换直接赋值
selectChange(val){
// form.postId = null
this.$set(this.form, 'postId', null)
}
10、截取字符串
let str = 'abcdef';
// 0
str = str.slice(0);//返回整个字符串 abcdef
str = str.substring(0);//返回整个字符串 abcdef
str = str.substr(0);//返回整个字符串 abcdef
// 使用一个参数
str = str.slice(2);//截取第二个之后所有的字符 cdef 即从第三位开始
str = str.substring(2);//截取第二个之后所有的字符 cdef
str = str.substr(2);//截取第二个之后所有的字符 cdef
// 使用两个参数
str = str.slice(2,4);//截取第二个到第四个之间的字符 cd
str = str.substring(2,4);//截取第二个到第四个之间的字符 cd
str = str.substr(2,4);//截取从第3个开始往后数4位之间的字符 cdef
// 使用两个负数
str = str.slice(1,-3);//截取第二个到第四个之间的字符 bc
str = str.substring(1,-3);//截取第二个到第四个之间的字符 a #负数转换为0
str = str.substr(1,-3);//不能为负数,若强行传递负数,会被当成0处理 ' ' #负数转换为0
console.log(str)
11、父组件值变子组件不能及时渲染:使用v-if销毁重置
12、列表按照整改期限进行排序
const showList = list.sort((item, item1) => {
return item1.rectificationLimit - item.rectificationLimit;
});