1.组件封装
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108<!-- 导入Excel组件 --> <template> <div> <el-button size="small" type="primary" @click="importOpen"> 导入 </el-button> <!-- 导入弹框 --> <el-dialog title="文件上传" :visible.sync="dialogImport"> <div> <el-upload :show-file-list="false" ref="importInfo" action="" :http-request="uploadFile" :on-change="handleChange" :auto-upload="false" > <el-input slot="trigger" v-model="importfilename" placeholder="请选择文件" readonly></el-input> <el-button type="success" @click="submitUpload">上传文件</el-button> </el-upload> </div> </el-dialog> </div> </template> <script> export default { name:"UploadExl", components:{ }, props:{ importFile:{ type:String, default(){ return "" } }, uploadExcel:{ type:Function, default(){ return } }, resSta:{ type:String, default(){ return "" } } }, data(){ return{ formData:{}, dialogImport:false, importfile:this.importfile, importfilename:"", resState, } }, watch:{ resSta(val){ this.resState=val this.$message( "导入成功" ), this.dialogImport=false this.$refs.importInfo.clearFiles() this.inportfilename="" } }, methods:{ importOpen(){ this.dialogImport=true }, uploadFile(param){ const formData=new FormData() formData.append(this.importfile,param.file) this.uploadExcel(this.formData) }, submitUpload(){ if(this.importfilename===""){ this.$message({ message:this.$t("请选择文件"), type:"error" }) }else{ this.$refs.importInfo.submit() } }, handleChange(){ this.importfilename="" let Xls=file.name.split("."); if(Xls[1]==="et"||Xls[1]==="xls"||Xls[1]==="xlsx"){ this.importfilename=file.name }else{ this.$message.error("上传文件只能是Excel文件") } } } } </script>
2.组件使用
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38<!-- 页面使用导入组件 --> <template> <div> <uploadXl :formdata="formdata" :import-file="importFile" :upload-excel="uploadExcel" :res-Sta="resSta" /> </div> </template> <script> import {uploadXl} from "../components/UploadXl" import { importEcl } from "../api/..." //请求方法 export default { components:{ uploadXl }, data(){ return{ importFile:"文件名", formdata:{}, resSta:"0" //弹框的状态 } }, methods:{ uploadExcel(file){ importEcl(file).then(response=>{ if(response.resCode===0){ this.resSta="1" this.findList() //重新调用请求方法 } }) } } } </script>
最后
以上就是典雅红酒最近收集整理的关于Vue导入excel功能封装的全部内容,更多相关Vue导入excel功能封装内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复