vue项目实现 导航栏 的方式

实现效果:

方式一:利用router-link实现

首先:新建一个组件,将头部导航栏提取出来,使多个组件可以公用一个头部。

代码如下: 

<template>
  <div>
    <div class="xl-flex" style="margin-left: 560px">
      <router-link class="title_change" to="/">
        <div class="titleIcon1" style="width: 24px; height: 24px"></div>
        <span class="titleSpan">首页</span>
      </router-link>
      <router-link class="title_change" to="/project-detail-all">
        <div class="titleIcon6" style="width: 24px; height: 24px"></div>
        <span class="titleSpan">项目</span>
      </router-link>
      <router-link class="title_change" to="/evaluate">
        <div class="titleIcon6" style="width: 24px; height: 24px"></div>
        <span class="titleSpan">后评价模块</span>
      </router-link>
    </div>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
    name: "title-index",
    data() {
        return {
        };
    }
};
</script>

<style scoped lang="less">
.title_change {
  display: flex;
  align-items: center;
  padding: 4px 16px;
  box-sizing: border-box;
  cursor: pointer;
  margin-right: 76px;
  text-decoration: none;
  .titleSpan {
    font-size: 18px;
    font-family: PingFang SC;
    font-weight: 500;
    line-height: 0px;
    color: #00d8ff;
  }
  .titleIcon1 {
    background: url("@/assets/images/icon/title-home-icon.png") center no-repeat;
    background-size: 100%;
  }
  .titleIcon2 {
    background: url("@/assets/images/icon/title-comprehensiveReview-icon.png")
      center no-repeat;
    background-size: 100%;
  }
  .titleIcon3 {
    background: url("@/assets/images/icon/title-powerAccess-icon.png") center
      no-repeat;
    background-size: 100%;
  }
  .titleIcon4 {
    background: url("@/assets/images/icon/title-plan-icon.png") center no-repeat;
    background-size: 100%;
  }
  .titleIcon5 {
    background: url("@/assets/images/icon/title-investManege-icon.png") center
      no-repeat;
    background-size: 100%;
  }
  .titleIcon6 {
    background: url("@/assets/images/icon/title-allPlan-icon.png") center
      no-repeat;
    background-size: 100%;
  }
}
.router-link-active { // 控制点击router-link之后的样式
  
  background: url("../../assets/images/icon/fiveWord-chosed-background.png")
    center no-repeat;
  background-size: 100% 100%;
  .titleSpan {
    color: #ff942c;
  }
  .titleIcon1 {
    background: url("@/assets/images/icon/title-home-chosed-icon.png") center
      no-repeat;
    background-size: 100%;
  }
  .titleIcon2 {
    background: url("@/assets/images/icon/title-comprehensiveReview-chosed-icon.png")
      center no-repeat;
    background-size: 100%;
  }
  .titleIcon3 {
    background: url("@/assets/images/icon/title-powerAccess-chosed-icon.png")
      center no-repeat;
    background-size: 100%;
  }
  .titleIcon4 {
    background: url("../../assets/images/icon/title-plan-chosed-icon.png")
      center no-repeat;
    background-size: 100%;
  }
  .titleIcon5 {
    background: url("@/assets/images/icon/title-investManege-chosed-icon.png") center
      no-repeat;
    background-size: 100%;
  }
  .titleIcon6 {
    background: url("@/assets/images/icon/title-allPlan-chosed-icon.png") center
      no-repeat;
    background-size: 100%;
  }
}
</style>

注意事项:

 使用的方法:

方式二:通过点击事件控制index,从而实现点击效果

代码如下:

<template>
  <div>
    <div>
      <div
        style="
          margin-left: 560px;
          display: flex;
          align-items: center;
          height: 87px;
          padding: 17px 0;
          box-sizing: border-box;
          cursor: pointer;
        "
      >
        <div
          @click="chosedTitle(item.num)"
          v-for="(item, index) in titleData"
          :key="index"
          style="
            display: flex;
            justify-content: center;
            align-items: center;
            margin-right: 76px;
          "
          :class="{
            twoWordChosedBackground:
              item.num == chosedIndex && item.title.length == 2,
            fourWordChosedBackground:
              item.num == chosedIndex && item.title.length == 4,
            fiveWordChosedBackground:
              item.num == chosedIndex && item.title.length == 5,
          }"
        >
          <img
            :src="item.num == chosedIndex ? item.chosedIcon : item.icon"
            style="width: 24px; height: 24px"
            alt=""
          />
          <span
            style="
              font-size: 18px;
              font-family: PingFang SC;
              font-weight: 500;
              line-height: 0px;
            "
            :style="{ color: item.num == chosedIndex ? '#FF942C' : '#00d8ff' }"
            >{{ item.title }}</span
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
    name:"title-index",
    data() {
        return {
            chosedIndex: 0,
            titleData: [
                {
                    title: "首页",
                    icon: require("@/assets/images/icon/title-home-icon.png"),
                    chosedIcon: require("@/assets/images/icon/title-home-chosed-icon.png"),
                    num: 0,
                },
                {
                    title: "综合评审",
                    icon: require("@/assets/images/icon/title-comprehensiveReview-icon.png"),
                    chosedIcon: require("@/assets/images/icon/title-comprehensiveReview-chosed-icon.png"),
                    num: 1,
                },
                {
                    title: "电源接入",
                    icon: require("@/assets/images/icon/title-powerAccess-icon.png"),
                    chosedIcon: require("@/assets/images/icon/title-powerAccess-chosed-icon.png"),
                    num: 2,
                },
                {
                    title: "月度计划",
                    icon: require("@/assets/images/icon/title-plan-icon.png"),
                    chosedIcon: require("@/assets/images/icon/title-plan-chosed-icon.png"),
                    num: 3,
                },
                {
                    title: "投资管理",
                    icon: require("@/assets/images/icon/title-investManege-icon.png"),
                    chosedIcon: require("@/assets/images/icon/title-investManege-chosed-icon.png"),
                    num: 4,
                },
                {
                    title: "项目全流程",
                    icon: require("@/assets/images/icon/title-allPlan-icon.png"),
                    chosedIcon: require("@/assets/images/icon/title-allPlan-chosed-icon.png"),
                    num: 5,
                },
            ],
        };
    },
    methods: {
        chosedTitle(val){ // 点击选中的值
            this.chosedIndex = val;
        },
    }
};
</script>

<style scoped>
.fiveWordChosedBackground{
     width: 146px;
    height: 32px;
    background: url('../../assets/images/icon/fiveWord-chosed-background.png') center no-repeat;
    background-size: 100%
}
.twoWordChosedBackground{
    width: 92px;
    height: 32px;
    background: url('../../assets/images/icon/twoWord-chosed-background.png') center center no-repeat;
    background-size: 100%
}
.fourWordChosedBackground{
    width: 128px;
    height: 32px;
    background: url('../../assets/images/icon/fourWord-chosed-background.png') center center no-repeat;
    background-size: 100%
}
</style>

此方法过于笨拙,不建议使用此方法。

  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue项目中,左侧导航栏通常是通过使用组件来实现的。根据引用[1]中的代码,可以看到在页面中使用了`<template>`标签来定义导航栏的结构。其中,`<el-aside>`标签用于显示一级菜单下的二级菜单,而`<el-main>`标签用于显示二级菜单对应的页面。在`<script>`标签中,通过引入`SideMenu`组件,并在`components`属性中注册该组件,可以实现导航栏的功能。同时,在`data`属性中定义了一个`lists`数组,用于存储菜单的数据。每个菜单项都包含一个`title`属性和一个`children`属性,`children`属性存储了二级菜单的数据。通过在模板中使用`SideMenu`组件,并将`lists`数组作为参数传递给该组件,可以动态生成导航菜单。[1] 如果需要使用动态数据构建导航菜单,可以参考引用[2]中的代码。在`action.js`文件中,通过配置获取动态树数据的请求路径,可以从后端获取动态数据。然后,在`LeftAside.vue`组件中,可以通过接口获取数据,并将数据传递给导航菜单组件,以构建动态导航菜单。[2] 另外,如果需要实现分页功能,可以参考引用[3]中的代码。在模板中使用`<el-pagination>`标签,通过设置相应的属性和事件来实现分页功能。可以设置`current-page`属性来指定当前页,`page-size`属性来设置每页显示的记录数,`total`属性来设置总记录数。通过监听`size-change`事件和`current-change`事件,可以在每页显示的记录数变化和当前页码变化时进行相应的处理。[3] 综上所述,通过使用组件和相应的属性、事件来实现左侧导航栏、动态导航菜单和分页功能,可以满足Vue项目中左侧导航栏的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值