<template>
<div class="hellos">
<div class="top">
<el-upload class="avatar-uploader"
action="上传头像接口"
name="upfile"
:show-file-list="false"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload">
<img v-if="imageUrl" :src="imageUrl" class="avatar">
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
<img :src="avatars" class="myheadimg"/>
</div>
</div>
</template>
<script>
import cookies from 'js-cookie';
import qs from 'qs';
export default {
name: 'HelloWorld',
data() {
return {
imageUrl: '',
avatars: "",
}
},
methods: {
handleAvatarSuccess(res, file) {
this.imageUrl = URL.createObjectURL(file.raw);
// console.log(res.pic_url)
this.upimg(res.pic_url)
},
beforeAvatarUpload(file) {
},
getimg() {
var url = "获取用户信息"
var params = {
user_id:cookies.get("token")
}
params=qs.stringify(params)
this.axios.post(url,params).then((res)=>{
// console.log(res.data.userInfo.avatar)
this.avatars=res.data.userInfo.avatar
})
},
upimg(myavatar) {
var url = "更新后的头像"
var params = {
user_id:cookies.get('token'),
avatar:myavatar
}
params=qs.stringify(params)
this.axios.post(url,params).then((res)=>{
this.getimg()
})
}
},
created() {
this.getimg()
}
}
</script>