1.部署elementUI:
cd到当前项目 运行 npm i element-ui -S
安装好了element,然后在项目里面引用,打开src目录下的main.js:
import ElementUI from 'element-ui'
Vue.use(ElementUI)
2.代码实例:
<el-upload class="upload-demo" ref="upload" action="../a/b/c" :before-upload="beforeUpload" :file-list="list" :auto-upload="false" :multiple="false">
<el-button size="small" type="primary" class="glyphicon glyphicon-import"></el-button>
<div slot="tip" class="el-upload__tip"></div>
</el-upload>
beforeUpload: function(file) {
//console.log(file)
//将文件转化为formdata数据上传
let fd = new FormData();
fd.append('file', file);
/*接口请求*/
this.$http.post('../a/b/c', fd, {
header: {},
emulateJSON: true
}).then((res) => {
//console.info(res);
if(res.body.code == "0") {
this.getData();
}else{
this.warnMessage = "导入失败";
this.colorMessage = "red"
}
}).catch(err => {
this.warnMessage = "访问接口失败";
this.colorMessage = "red"
})
return false;
},