美文网首页
(二十二)VueCli3.0全栈——设置顶部导航和下拉菜单

(二十二)VueCli3.0全栈——设置顶部导航和下拉菜单

作者: 彼得朱 | 来源:发表于2019-07-10 20:41 被阅读0次

效果:

效果图

1、client/src/components下新建HeadNav.vue

<template>
  <header class="head-nav">
    <el-row>
      <el-col :span="6" class="logo-container">
        <img src="../assets/logo.png" class="logo" alt />
        <span class="title">购课在线后台管理系统</span>
      </el-col>
      <el-col :span="6" class="user">
        <div class="userinfo">
          <img class="" :src="user.avatar" alt />
          <div class="welcome">
            <p class="name comename">欢迎</p>
            <p class="name avatarname">{{user.name}}</p>
          </div>
          <span class="username">
            <el-dropdown trigger="click" @command="setDialogInfo" >
              <span class="el-dropdown-link">
                <i class="el-icon-caret-bottom el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="info">个人信息</el-dropdown-item>
                <el-dropdown-item command="logout">退出</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </span>
        </div>
      </el-col>
    </el-row>
  </header>
</template>

<script>
export default {
  name: "head-nav",
  computed: {
      user(){
          return this.$store.getters.user 
      }
  },
  methods: {
      setDialogInfo(cmdItem){
        //   console.log(cmdItem);
        switch(cmdItem){
            case "info":
                this.showInfoList();
                break;
            case "logout":
                this.logout();
                break;
        }
      },
      showInfoList(){
          console.log("个人信息");
      },
      logout(){
        //   清除token
        localStorage.removeItem("eleToken");
        // 设置vuex store
        this.$store.dispatch('clearCurrentState');
        // 跳转到登录页面
        this.$router.push('/login');

      }
  }
};
</script>



<style scoped>
.head-nav {
  width: 100%;
  height: 60px;
  min-width: 600px;
  padding: 5px;
  background: #324057;
  color: #fff;
  border-bottom: 1px solid #1f2d3d;
}

.logo-container {
  line-height: 60px;
  min-width: 400px;
}
.logo {
  height: 50px;
  width: 50px;
  margin-right: 5px;
  vertical-align: middle;
  display: inline-block;
}
.title {
  vertical-align: middle;
  font-size: 22px;
  font-family: 'Microsoft YaHei';
  letter-spacing: 3px;
}

.user {
    line-height: 60px;
    text-align: right;
    float: right;
    padding-right: 10px;
}
.user img{
    width: 50px;
    height: 50px;
    border-radius: 50%;
}
.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  vertical-align: middle;
  display: inline-block;
}
.welcome {
  display: inline-block;
  width: auto;
  vertical-align: middle;
  padding: 0 5px;
}

.name {
  line-height: 20px;
  text-align: center;
  font-size: 14px;
}
.comename {
  font-size: 12px;
}
.avatarname {
  color: #409eff;
  font-weight: bolder;
}

.username {
  cursor: pointer;
  margin-right: 5px;
}

.el-dropdown{
    color:#fff;

}
</style>



2、Index.vue中引入和使用 (client/src/views/Index.vue)

<template>
    <div class="index">
        <HeadNav></HeadNav>
    </div>
</template>

<script>
import HeadNav from '../components/HeadNav'
export default {
    name:'index',
    components: {
        HeadNav
    }
}
</script>

3、退出登录时,设置了一个方法把vuex中存储的信息给清除掉了

在client/src/store.js中新增action方法:

//   清除当前状态
  clearCurrentState:({commit})=>{
    commit(types.SET_AUTHENTICATED,false);
    commit(types.SET_USER,null);
  }
store.js里面新增的

顶部导航和下拉菜单完成了。

相关文章

网友评论

      本文标题:(二十二)VueCli3.0全栈——设置顶部导航和下拉菜单

      本文链接:https://www.haomeiwen.com/subject/ygmkkctx.html