项目中energyType数组后端无法接收,要转变成字符串传给后端,组件图片如下
组件代码如下
操作台打印数据如下
<template>
<div class="focus_tab_pane">
<div class="approval">
<div class="title flex justify-between align-center">
<el-button
type="text"
size="medium"
style="color:#E6A23C"
@click="download"
>
样本信息下载
</el-button>
</div>
</div>
<!-- 样本信息下载 -->
<el-dialog
:visible="isVisible"
width="400px"
title="类型选择"
:before-close="closeDialog"
:close-on-click-modal="false"
>
<el-form
ref="searchForm"
:model="searchForm"
:rules="searchFormRules"
label-width="100px"
>
<el-form-item label="年度" prop="year">
<el-date-picker
v-model="searchForm.year"
type="year"
value-format="yyyy"
:clearable="false"
placeholder="请选择"
/>
</el-form-item>
<el-form-item label="时期" prop="period">
<el-select v-model="searchForm.period" placeholder="请选择">
<el-option label="上半年" value="0" />
<el-option label="下半年" value="1" />
</el-select>
</el-form-item>
<el-form-item label="营运类型" prop="energyType">
<el-select v-model="searchForm.energyType" multiple placeholder="请选择">
<el-option label="公路客运" value="1" />
<el-option label="公路货运" value="2" />
<el-option label="出租客运" value="3" />
<el-option label="公交公司" value="4" />
<el-option label="水路客运" value="5" />
<el-option label="水路货运" value="6" />
<el-option label="港口企业" value="7" />
</el-select>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button size="medium" @click="closeDialog">取消</el-button>
<el-button
size="medium"
type="primary"
:loading="apiLoading"
@click="validateForm"
>
确认
</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import { noSay } from '@/api/focus'
import { convertRes2Blob, getYear } from '@/api/public'
export default {
name: 'ListTabPane',
props: {
status: {
type: Number,
required: true
}
},
data() {
return {
loading: false,
showMore: false,
natureTypeList: [],
list: [],
matchingNumberList: [],
searchForm: {
year: null,
period: null,
energyType: []
},
searchFormRules: {
year: [{ required: true, trigger: 'change', message: '请选择年度' }],
period: [{ required: true, trigger: 'change', message: '请选择时期' }],
energyType: [{ required: true, trigger: 'change', message: '请选择营运类型' }]
},
isVisible: false,
apiLoading: false,
period: null,
year: null,
energyType: []
}
},
created() {
const natureTypeList = []
this.matchingList.forEach(element => {
this.$store.getters.companys.natureTypeList.forEach(item => {
if (element.type === item.natureTypeCode) {
natureTypeList.push(element)
}
})
})
this.natureTypeList = natureTypeList
this.getTypeList()
this.handleYear()
},
methods: {
async handleYear() {
const res = await getYear()
console.log(res)
this.period = res.period
this.year = res.year
this.energyType = res.energyType
},
download() {
this.isVisible = true
},
closeDialog() {
this.isVisible = false
this.apiLoading = false
this.$refs['searchForm'].resetFields()
},
validateForm() {
this.$refs['searchForm'].validate(valid => {
valid && this.submitForm()
})
},
submitForm() {
this.apiLoading = true
noSay({ ...this.searchForm }).then(res => {
convertRes2Blob(res, '样本信息')
setTimeout(() => {
this.apiLoading = false
this.closeDialog()
})
})
}
}
}
</script>
// 样本信息下载
export function noSay(params) {
return request({
url: 'energy/excel/export',
method: 'POST',
responseType: 'blob',
data: params
})
}
// 年度查询
export function getYear() {
return request({
url: 'energy/enterpriseSampleInfo/time',
method: 'GET'
})
}
就是要把energyType从数组转变为字符串,用‘,’隔开,我之前写的如下
methods: {
async handleYear() {
const params = JSON.parse(JSON.stringify(this.form))
params.energyType = params.energyType.join(',')
const res = await getYear()
console.log(res)
this.period = res.period
this.year = res.year
this.energyType = res.energyType
},
}
报错TypeError: Cannot read properties of undefined (reading 'join')