vue提交表单时忽略null的变量

使用场景如:添加和修改是同一个表单同一个api地址,添加数据时忽略id字段

1.安装并导入 lodash的 pickBy

 

npm install lodash
# 或
yarn add lodash

2.在main.js项目中引入

import { pickBy } from 'lodash'; // 正确导入 pickBy

Vue.prototype.$pickBy = pickBy

image

 

 

3.使用示例(其中this.currentData是需要过滤的数组,cleanedData 是过滤后的数组):

let cleanedData = this.$pickBy(this.currentData, value => value !== null && value !== undefined);

 

image

 

posted @ 2025-10-13 19:38  幽暗天琴  阅读(4)  评论(0)    收藏  举报