4、前端导航栏组件
Header.vue代码修订
<template>
<div class="header-box">
<div class="header">
<div class="content">
<div class="logo full-left">
<router-link to="/"><img @click="jump('/')" src="@/assets/img/logo.svg" alt=""></router-link>
</div>
<ul class="nav full-left">
<li v-for="nav in nav_list" :key="nav.name">
<span @click="jump(nav.link)" :class="this_nav==nav.link?'this':''">{{nav.name}}</span>
</li>
</ul>
<!-- 改造的部分 -->
<div v-if="token" class="login-bar full-right">
<div class="shop-cart full-left">
<img src="@/assets/img/cart.svg" alt="">
<span><router-link to="/cart">购物车</router-link></span>
</div>
<div class="login-box login-box1 full-left">
<router-link to="">学习中心</router-link>
<el-menu width="200" id="member" class="el-menu-demo full-right" mode="horizontal">
<el-submenu index="2">
<template slot="title">
<router-link to=""><img src="@/assets/img/logo@2x.png" alt=""></router-link>
</template>
<el-menu-item index="2-1">我的账户</el-menu-item>
<el-menu-item index="2-2">我的订单</el-menu-item>
<el-menu-item index="2-3"><span @click="logout">退出登录</span></el-menu-item>
</el-submenu>
</el-menu>
</div>
</div>
<div v-if="!token" class="login-bar full-right">
<div class="shop-cart full-left">
<img src="@/assets/img/cart.svg" alt="">
<span><router-link to="/cart">购物车</router-link></span>
</div>
<div class="login-box login-box2 full-left">
<router-link to="/login"><span>登录</span></router-link>
|
<router-link to="/register"><span>注册</span></router-link>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: "Header",
data() {
return {
token: localStorage.token || sessionStorage.token,
this_nav: "",
nav_list: [],
}
},
created() {
this.this_nav = localStorage.this_nav;
let _this = this;
// 获取服务端的导航信息
this.$axios({
url: this.$settings.Host + "home/nav/header/"
}).then(function (response) {
_this.nav_list = response.data
}).catch(function (error) {
window.console.log(error)
});
},
methods: {
jump(location) {
localStorage.this_nav = location;
// vue-router除了提供router-link标签跳转页面以外,还提供了 js跳转的方式
this.$router.push(location);
},
// 改造的部分
logout() {
// 重置token
this.token = false;
// 移除用户登录信息
sessionStorage.clear();
localStorage.clear();
// 退出登录提示
let _this = this;
_this.$message({
message: '退出登录成功!',
type: 'success',
duration: '1500',
onClose() {
// 跳转到首页
_this.$router.push("/");
}
});
}
},
}
</script>
<style scoped>
.header-box {
height: 80px;
}
.header {
width: 100%;
height: 80px;
box-shadow: 0 0.5px 0.5px 0 #c9c9c9;
position: fixed;
top: 0;
left: 0;
right: 0;
margin: auto;
z-index: 99;
background: #fff;
}
.header .content {
max-width: 1200px;
width: 100%;
margin: 0 auto;
}
.header .content .logo {
height: 80px;
line-height: 80px;
margin-right: 50px;
cursor: pointer; /* 设置光标的形状为爪子 */
}
.header .content .logo img {
vertical-align: middle;
}
.header .nav li {
float: left;
height: 80px;
line-height: 80px;
margin-right: 30px;
font-size: 16px;
color: #4a4a4a;
cursor: pointer;
}
.header .nav li span {
padding-bottom: 16px;
padding-left: 5px;
padding-right: 5px;
}
.header .nav li span a {
display: inline-block;
}
.header .nav li .this {
color: #4a4a4a;
border-bottom: 4px solid #ffc210;
}
.header .nav li:hover span {
color: #000;
}
.header .login-bar {
height: 80px;
}
.header .login-bar .shop-cart {
margin-right: 20px;
border-radius: 17px;
background: #f7f7f7;
cursor: pointer;
font-size: 14px;
height: 28px;
width: 88px;
margin-top: 30px;
line-height: 32px;
text-align: center;
}
.header .login-bar .shop-cart:hover {
background: #f0f0f0;
}
.header .login-bar .shop-cart img {
width: 15px;
margin-right: 4px;
margin-left: 6px;
}
.header .login-bar .shop-cart span {
margin-right: 6px;
}
.header .login-bar .login-box {
margin-top: 33px;
}
.header .login-bar .login-box span {
color: #4a4a4a;
cursor: pointer;
}
.header .login-bar .login-box span:hover {
color: #000000;
}
/*改造的部分*/
#member {
border: none;
margin-top: -22px;
}
#member img {
width: 26px;
height: 26px;
border-radius: 50%;
}
</style>
程序猿,要对自己狠一点!