码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Vue + Element-UI —— 项目实战(六)


    系列文章目录

    Vue + Element-UI —— 项目实战(零)(项目概述)

    Vue + Element-UI —— 项目实战(一)

    Vue + Element-UI —— 项目实战(二)

    Vue + Element-UI —— 项目实战(三)

    Vue + Element-UI —— 项目实战(四)

    Vue + Element-UI —— 项目实战(五)

    Vue + Element-UI —— 项目实战(六)

    Vue + Element-UI —— 项目实战(七)

    Vue + Element-UI —— 项目实战(八)(完结)

    文章目录

    • 系列文章目录
        • 六、项目实战六
          • Ⅰ、面包屑切换功能
            • 1. 面包屑数据处理
            • 2. 使用 vuex 进行通信
          • Ⅱ、Tag 切换功能
            • 1. tag 页面
            • 2. 关闭标签函数
            • 3. 主页面展示


    六、项目实战六

    Ⅰ、面包屑切换功能

    1. 面包屑数据处理
    1. 在 views 文件夹下创建 ./mall/index.vue 和 ./other/pageOne.vue 和 ./other/pageTwo.vue 文件

    在这里插入图片描述

    1. 在 ./router/index.js 中配置路由
    	{
          path: "/mall",
          name: "mall",
          component: mall,
        },
        {
          path: "/page1",
          name: "page1",
          component: pageOne,
        },
        {
          path: "/page2",
          name: "page2",
          component: pageTwo,
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    2. 使用 vuex 进行通信
    1. 在 ./store/tab.js 中进行相关配置(配置初始数据)
    	state: {
    	  // 定义首页导航初始数据(面包屑)
    	  tabsList: [
    	    {
    	      path: '/',
    	      name: 'home',
    	      label: '首页',
    	      icon: 'home'
    	    }
    	  ],   
    	  // 是否高亮
    	  currentMenu: null,
    	  menu: []
    	},
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    1. 在 ./store/tab.js 中进行相关配置(配置 mutations)
    	mutations: {
    	  // 改变state中的tabsList数据
          selectMenu(state, val) {
            // 当前点击的菜单不是首页
            if(val.name !== 'home') {
              // currentMenu 等于传入进来的数据
              state.currentMenu = val
              // 拿到当前选中项的索引(看看存不存在)
              const result = state.tabsList.findIndex(item => item.name === val.name)
              // 不存在的时候添加数据
              if(result === -1) {
                state.tabsList.push(val)
              }
            }else{
              // 是首页就对当前选中的标识重置
              state.currentMenu = null
            }
          },
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    1. 在 CommonAside.vue 中添加点击事件
    	clickMenu(item) {
          // 点击进行页面跳转 使用的是push方法
          this.$router.push({
            name: item.name,
          });
          // item为对应的menu的数据
          this.$store.commit('selectMenu', item)
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. 在 CommonHeader.vue 中注入数据
    	<!-- 面包屑 -->
        <el-breadcrumb separator="/">
          <!-- 遍历面包屑的数据 -->
          <el-breadcrumb-item v-for="item in tags" :key="item.path" :to="{ path: item.path }">{{item.label}}</el-breadcrumb-item>
        </el-breadcrumb>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    	computed: {
    	  ...mapState({
    	    // 面包屑数据
    	    tags: state => state.tab.tabsList
    	  })
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    Ⅱ、Tag 切换功能

    1. tag 页面
    1. 创建 CommonTag.vue 文件
    <template>
      <div class="tabs">
        <!-- 
          :closable:除了首页以外的tag都可关闭
          :effect:主题,路由和tag名称一样的显示高亮
         -->
        <el-tag
          v-for="(tag, index) in tags"
          :key="tag.name"
          :closable="tag.name !== 'home'"
          :effect="$route.name === tag.name ? 'dark' : 'plain'"
          @click="changeMenu(tag)"
          @close="handleClose(tag, index)"
          size="small"
        >
          <!-- 显示对应的名称 -->
          {{tag.label}}
        </el-tag>
      </div>
    </template>
    
    <script>
    import {mapState, mapMutations} from 'vuex'
    export default {
        name: 'CommonTag',
        data() {
          return {}
        },
        computed: {
          ...mapState({
            // tags对应vuex中的tabsList
            tags: state => state.tab.tabsList
          })
        },
        methods:{
          ...mapMutations({
            close: 'closeTag'
          }),
          changeMenu(item){
            // 路由的切换
            this.$router.push({name: item.name})
          },
          handleClose(tag, index) {
            // 拿到tag的总长度
            const length = this.tags.length - 1
            // 调用mutations里面的closeTag方法
            this.close(tag)
            // 删除的tag名字不是当前的路由名字,则不用删除
            if(tag.name !== this.$route.name){
              return;
            }
            // 最右侧的tag
            if(index === length) {
              this.$router.push({
                name: this.tags[index-1].name //向左跳转
              })
              // 删除的tag在中间,向右跳转
            } else {
              this.$router.push({
                name: this.tags[index].name
              })
            }
          }
        }
    };
    </script>
    
    <style lang="less" scoped>
    .tabs {
      padding: 20px;
      .el-tag {
        margin-right: 15px;
        cursor: pointer;
      }
    }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    2. 关闭标签函数
    1. 在 tab.js 的 mutations 中定义关闭 tag 的函数
    	closeTag(state, val) {
          // 找到当前的标签
          const result =  state.tabsList.findIndex(item => item.name === val.name)
          // 将它从数据源中删掉
          state.tabsList.splice(result, 1)
        },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    3. 主页面展示
    1. 在 main.js 中的 头部 和 主体 部分之间显示该部分组件
    	<el-container>
    	  <el-header>
            <common-header></common-header>
          </el-header>
    
          <!-- 头部下面的编辑标签 -->
          <common-tag></common-tag>
    
          <!-- 主体部分 -->
          <el-main>
            <router-view></router-view>
          </el-main>
    	</el-container>
    
    	// 在 Main.vue 中导入tag公共样式
    	import CommonTag from "@/components/CommonTag.vue";
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    不积跬步无以至千里 不积小流无以成江海

    点个关注不迷路(持续更新中…)

  • 相关阅读:
    java版直播商城免费搭建平台规划及常见的营销模式+电商源码+小程序+三级分销+二次开发
    uniapp实现发送获取验证码
    ShardingSphere实现读写分离
    基于先验激光雷达地图的2D-3D线特征单目定位
    遥感SCI期刊模板下载教程———IEEE TGRS、GRSL、JSTARS
    ROS2进阶第三章 -- 机器人语音交互之ros集成科大讯飞中文语音库,实现语音控制机器人小车
    神经网络前向和后向传播推导(一):概览
    【ESP 保姆级教程】疯狂Node.js服务器篇 ——案例:ESP8266 + 环境监测 +NodeJs本地服务+文件存储数据 + 钉钉/微信/飞书报警
    创建对象在堆区如何分配内存
    mybatis增删改查模板设置及设置调用
  • 原文地址:https://blog.csdn.net/qq_45902692/article/details/125191767
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号