从0开始写Vue项目-Vue实现用户个人信息界面上传头像

一、前言

都知道,我们在写项目的时候,最想给自己项目增加实用性了,例如我们的头像上传以及个人的各种信息等等。我们刚刚已经做好了我们的上传接口,那么我们现在就趁热打铁,将我们的头像上传功能解决一下。

二、个人信息界面

我们的个人信息界面由我们的头像以及我们的表格组成,那么就说明我们需要去获取我们用户的数据了

我们之前是做过用户数据存储的,就是利用token来将我们用户的数据存放在我们的浏览器里面,然后利用localStorage来查找出我们的用户数据。

三、头像上传

对于头像上传,我们依然用到我们之前的接口和组件来实现该功能。

四、头部Header使用

我们在我们的头部进行数据查询和引用,就是通过user,然后找到我们的头像的元素,最后进行展示

我们需要在我们的脚手架里面去获取用户User最新的数据,然后最后在我们的头部Header里面去进行引入并获取信息

源码:

<template>
  <div style="display: flex; line-height: 60px">
    <div style="flex: 1">
      <span :class="collapseBtnClass" style="cursor: pointer; font-size: 18px"></span>
      <el-breadcrumb separator="/" style="display: inline-block; margin-left: 10px">
        <el-breadcrumb-item :to="/mall/index" >主页</el-breadcrumb-item>
        <el-breadcrumb-item >{
         
  { currentPathName }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <el-dropdown style="width: 130px; cursor: pointer">

      <div style="display: inline-block; float: right; margin-right: 10px">
        <img :src="user.avatarUrl" alt=""
             style="width: 30px; border-radius: 50%; position: relative; top: 10px; right: 5px">
        <span>{
         
  {user.nickname}}</span><i class="el-icon-arrow-down" style="margin-left: 5px"></i>
      </div>

      <el-dropdown-menu slot="dropdown" style="width: 100px; text-align: center">
        <el-dropdown-item style="font-size: 14px; padding: 5px 0">
          <span style="text-decoration: none" @click="person">个人信息</span>
        </el-dropdown-item>
        <el-dropdown-item style="font-size: 14px; padding: 5px 0">
          <span style="text-decoration: none" @click="logout">退出登录</span>
        </el-dropdown-item>
      </el-dropdown-menu>

    </el-dropdown>

  </div>
</template>

<script>
export default {
  name: "Header",
  props: {
    collapseBtnClass: String,
    user: Object
  },
  computed: {
    currentPathName () {
      return this.$store.state.currentPathName;  //需要监听的数据
    }
  },
  data() {
    return {
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  methods: {
    logout() {
      this.$router.push("/login")
      this.$message.success("退出成功")
    },
    person(){
      this.$router.push("/mall/person")
    }
  }
}
</script>

<style scoped>

</style>

其他请见:

⛵小结

经验分享 程序员 微信小程序 职场和发展