For previous "Apply ten: Vue Vue and TypeScript of integrated development," the introduction, here to share a package grammar import component of the project in the near future, the reference component source familiar .vue to better document the script.
Component Source:
<template> <div id="myImport"> <el-button type="primary" plain @click="importDialogVisible = true">导入</el-button> <!-- 导入弹窗 --> <el-dialog :title="name + '导入'" :visible.sync="importDialogVisible" width="560px" :close-on-click-modal="false" @close="fileList = []"> <= "Importing"Element-loading-text= "loading"V-loadingdiv> <div style="color: #2D2D2D;margin-bottom: 20px;">导入文件</div> <el-upload class="upload-demo" ref="upload" :action="url" name="file" :headers="importHeaders" :on-change="changeUpload" :on-success="uploadSuccess" :file-list="fileList" :limit="1" :multiple="false" :auto-upload="false" > <el-button slot="trigger" type="primary">选取文件</el-button> <div slot="tip" class="el-upload__tip"></div> </el-upload> <br> <div v-if="templateUrl"><span class="download-template" @click="templateDownload">{{name + '模板下载'}}</span></div> <el-button class="import-btn" type="primary" @click="submitUpload">导入</el-button> </div> </el-dialog> </div> </template> <script lang="ts"> import { Component, Prop, Emit, Vue, Ref } from 'vue-property-decorator' @Component export default class MyImport extends Vue { @Prop({ default: '' }) private url!: string //Import interface address @Prop ({ default : '' }) Private templateUrl ! : String // interface addresses, template @Prop ({ default : '' }) Private name ! : String Private loading: Boolean = to false Private importDialogVisible: Boolean = to false Private importHeaders: the any = { ' token ' : sessionStorage.getItem ( ' token ' )} Private the fileList: the Array < the any > = [] // 下载导入模板 public templateDownload() { // TODO } @Ref() readonly upload!: any public changeUpload(file: File, fileList: Array<any>) { let index: number = file.name.lastIndexOf('.') let suffix: string = file.name.substring(index + 1).toUpperCase() if (suffix !== 'XLS' && suffix !== 'XLSX') { this. Message.Error An $ ( ' only supports importing files execl ' ) the this .upload.clearFiles () return to false } the this .fileList = the fileList } public submitUpload () { IF ( the this .fileList.length == 0 ) { the this . $ Message.Error An ( ' select the file to import ' ) return to false } the this .loading = to true the this .upload.submit () } @Emit ( ' uploadSuccess to ') public uploadSuccess(res: any) { this.loading = false this.importDialogVisible = false; return res } } </script> <style lang="scss"> #myImport { display: inline-block; margin-left: 10px; .download-template { color: #5C5C5C; font-size: 12px; border-bottom: 1px solid #5C5C5C; cursor: pointer; } .import-btn { position: relative; right: -420px; } } </style>
If the script is not particularly understanding of grammar, you can go back and view vue-property-decorator official documents.
Components of use:
<div class="btn-form"> <el-button type="primary" @click="queryTableData">查询</el-button> <el-button type="primary" plain @click="clearFormData">重置</el-button> <el-button type="primary" plain @click="exportTableData">导出</el-button> <!--导入组件--> <my-import :url="url" :templateUrl="templateUrl" :name="'报废信息'" @uploadSuccess="uploadSuccess"></my-import> </div>
effect: