一颗努力的大土豆 2025-07-23 11:22 采纳率: 92.5%
浏览 17
已结题

Vue+Element-ui 如何在项目中加上tabs页,比如下面页面中有很多跳转页面,如何在表头那里(红框框住的部分)保留跳转页面的内容,可以直接根据这个tabs页或者面包屑进行切换

Vue+Element-ui 如何在项目中加上tabs页,比如下面页面中有很多跳转页面,如何在表头那里(红框框住的部分)保留跳转页面的内容,可以直接根据这个tabs页或者面包屑进行切换

问题详述

img

img

下面是面包屑导航栏封装的组件代码

<template>
  <div class="container">
    <!-- <i class="el-icon-map-location"></i> -->
    <el-breadcrumb class="app-breadcrumb" separator="/"> 
      <transition-group name="breadcrumb">
        <!-- <el-breadcrumb-item v-for="(item,index) in levelList" :key="item.path">
          <span v-if="item.redirect === 'noRedirect' || index == levelList.length - 1" class="no-redirect">{{ item.meta.title }}</span>
          <a v-else>{{ item.meta.title }}</a>
        </el-breadcrumb-item> -->
        <el-breadcrumb-item v-for="(item,index) in levelList" :key="item.path">
          <span v-if="item.redirect === 'noRedirect' || index == levelList.length - 1" class="no-redirect">{{ item.meta.title }}</span>
          <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
        </el-breadcrumb-item> 
      </transition-group>
    </el-breadcrumb>
  </div>
  
</template>

<script>
export default {
  data() {
    return {
      levelList: null
    }
  },
  watch: {
    $route(route) {
      // if you go to the redirect page, do not update the breadcrumbs
      if (route.path.startsWith('/redirect/')) {
        return
      }
      this.getBreadcrumb()
    }
  },
  created() {
    this.getBreadcrumb()
  },
  methods: {
    getBreadcrumb() {
      // only show routes with meta.title
      let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
      const first = matched[0]

      // if (!this.isDashboard(first)) {
      //   matched = [{ path: '/index', meta: { title: '首页' }}].concat(matched)
      // }

      this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
    },
    isDashboard(route) {
      const name = route && route.name
      if (!name) {
        return false
      }
      return name.trim() === 'Index'
    },
    handleLink(item) {
      const { redirect, path } = item
      if (redirect) {
        this.$router.push(redirect)
        return
      }
      this.$router.push(path)
    }
  }
}
</script>

<style lang="scss" scoped>
.container{
  display:flex;
  align-items: center;
  /* color: #bfebfa; */
  .app-breadcrumb.el-breadcrumb {
    display: inline-block;
    font-size: 14px;
    line-height: 50px;
    margin-left: 8px;

    .no-redirect {
      color: #515f77;
      cursor: text;
    }
  }
}
</style>



下面是一个页面代码(其他页面也有跳转)

 <!-- 虚拟网卡 -->
<template>
  <div class="app-container">
    <bread-crumb />

    <div class="container-bottom">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="60px" class="container-top">
        <el-form-item label="网卡名称" prop="vnicName" label-width="75px">
          <el-input
            v-model="queryParams.vnicName"
            placeholder="请输入网卡名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="网卡IP" prop="ipAddress">
          <el-input
            v-model="queryParams.ipAddress"
            placeholder="请输入网卡IP"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="MAC地址" prop="mac" label-width="75px">
          <el-input
            v-model="queryParams.mac"
            placeholder="请输入MAC地址"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button class="button-search" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button class="button-refresh" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:post:add']"
          >添加网卡</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:post:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:post:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange" :height="tableHeight"  style="background-color: transparent;" header-row-class-name="header-row-style">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" type="index" align="center">
          <template slot-scope="scope">
            <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column label="网桥名称" align="center" prop="brName" />
        <el-table-column label="网卡名称" align="center" prop="vnicName" />
        <el-table-column label="IP地址" align="center" prop="ipAddress" />
        <el-table-column label="子网掩码" align="center" prop="mask" />
        <el-table-column label="MAC地址" align="center" prop="mac" />
        <el-table-column label="MTU" align="center" prop="mtu" />
        <el-table-column label="状态" align="center" prop="status" width="120" >
          <template slot-scope="scope">
            <span v-if="scope.row.status == '1'">创建成功</span>
            <el-button
              v-if="scope.row.status != '1'"
              size="mini"
              type="text"
              @click="handleReAdd(scope.row)"
              v-hasPermi="['monitor:job:query']"
            >{{ scope.row.status == '0' ? '未创建,点此创建' : scope.row.status == '2' ? '创建失败,点此重建' : '' }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              class="del-btn"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:post:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    
    <!-- 添加或修改控制节点对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="网桥名称" prop="brName">
              <el-input v-model="form.brName" placeholder="请输入网桥名称" :disabled="editEnable"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="网卡名称" prop="vnicName">
              <el-input v-model="form.vnicName" placeholder="请输入网卡名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="IP地址" prop="ipAddress">
              <el-input v-model="form.ipAddress" placeholder="请输入IP地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="子网掩码" prop="mask">
              <el-input v-model="form.mask" placeholder="请输入子网掩码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="MAC地址" prop="mac">
              <el-input v-model="form.mac" placeholder="请输入MAC地址" style="width: calc(100% - 100px)" />
              <el-button type="text" size="mini" @click="handleMac()" style="margin-left: 10px">点击生成MAC</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="MTU" prop="mtu">
              <el-input-number :min="1" :max="1420" v-model="form.mtu" placeholder="MTU"  />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInterface, addInterface,reAddInterface,delInterface,delAllInterface } from "@/api/networkEquipment/networkInterface";
import breadCrumb from "@/components/Breadcrumb/index.vue"
import {listMac} from "@/api/logicalDevice/router";
import autoComputedH from '@/utils/mixins/autoComputedH'
export default {
  name: "Post",
  mixins: [autoComputedH],
  /* dicts: ['sys_normal_disable'], */
  components: {
    breadCrumb,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 控制节点表格数据
      postList: [],
      // 弹出层标题
      title: "",
      //所在设备下拉框
      deployEqpList: [],
      // 是否显示弹出层
      open: false,
      pageNum: 1,
      pageSize: 10,
      //编辑文本框是否禁用
      editEnable: true,
      // 查询参数
      queryParams: {
        vnicName: undefined,
        ipAddress: undefined,
        mac: undefined,
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {
        brName: undefined,
        vnicName: undefined,
        ipAddress: undefined,
        mask: undefined,
        mac: undefined, 
        mtu: undefined
      },
      // 表单校验
      rules: {
        brName: [
          { required: true, message: "网桥名称不能为空", trigger: "blur" }
        ],
        vnicName: [
          { required: true, message: "网卡名称不能为空", trigger: "blur" }
        ],
        ipAddress: [
          { required: true, message: "IP地址不能为空", trigger: "blur" }
        ],
        mask: [
          { required: true, message: "子网掩码不能为空", trigger: "blur" }
        ],
        mac: [
          { required: true, message: "MAC地址不能为空", trigger: "blur" }
        ],
        mtu: [
          { required: true, message: "MTU不能为空", trigger: "blur" }
        ]
      },
      gwEqpId:null,
      containerTop: null,
    };
  },
  created() {
    this.gwEqpId = localStorage.getItem('id');; 
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listInterface({ gwEqpId: this.gwEqpId, ...this.queryParams }).then(response => {
        this.postList = response.rows;
        console.log(response, '从接口获取的数据');
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gwEqpId: undefined,
        brName: "br-int",
        ipAddress: undefined,
        vnicName: undefined,
        mac: undefined, 
        mtu: 1420
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加网卡";
    },
    handleReAdd(row) {
      const params = {id:row.id};  
      this.$modal.confirm('是否确认重新创建?').then(()=> {
        return reAddInterface(params).then((response) => {
          if (response.success) {
            this.$modal.msgSuccess("调用成功");
            this.getList();
          } else {
            this.$modal.msgError( "调用失败");
          }
        });
      }).catch(() => {});
    },
    /** 修改按钮操作 */
    /* handleUpdate(row) {
      this.reset();
      const postId = row.postId || this.ids
      getPost(postId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改岗位";
      });
    }, */
    /** 提交按钮 */
  submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.mtu = Number(this.form.mtu);
          const formData = {
            gwEqpId: this.gwEqpId, 
            brName: this.form.brName,
            vnicName: this.form.vnicName,
            ipAddress: this.form.ipAddress,
            mask: this.form.mask,
            mac: this.form.mac,
            mtu: this.form.mtu
          };
          if (this.form.gwEqpId != undefined) {
            delControlNode(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInterface(formData).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const InterfaceId = {id:row.id || this.ids[0]};
      const InterfaceIds = {ids:this.ids}  
      if(this.ids.length<2){
        this.$modal.confirm('是否确认删除所选虚拟网卡数据项?').then(()=> {
        return delInterface(InterfaceId).then((response) => {
          if(response.success){
            this.postList = this.postList.filter(item => item.id !== row.id);
            this.$modal.msgSuccess("删除成功");
            this.getList();
          }else {
            this.$modal.msgError( "删除失败");
          }
      });
      }).catch(() => {
        
      });

      }else{
        this.$modal.confirm('是否确认删除所选虚拟网卡数据项?').then(()=> {
        return delAllInterface(InterfaceIds).then((response) => {
          if(response.success){
            this.postList = this.postList.filter(item => item.id !== row.id);
            this.$modal.msgSuccess("删除成功");
            this.getList();
          }else {
            this.$modal.msgError( "删除失败");
          }
      });
      }).catch(() => {
        
      });
      }  
      
    },
    handleMac(){
      listMac().then(response => {
        this.form.mac = response.data
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/vnic/export', {gwEqpId:this.gwEqpId,
        ...this.queryParams
      }, `虚拟网卡_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
</style>



  • 写回答

4条回答 默认 最新

  • 檀越@新空间 2025-07-23 11:23
    关注

    上午好☀️☀️☀️️
    本答案参考通义千问

    在使用 Vue + Element UI 构建项目时,若想在页面的顶部(如红框区域)添加 Tabs 页签,并且希望在切换 Tabs 时保留内容,同时可以通过 面包屑导航Tabs 页签 进行切换,那么你可以通过以下方式实现:


    ✅ 解决方案

    1. 引入并配置 el-tabsel-tab-pane

    在页面中使用 el-tabs 组件来创建可切换的标签页。每个标签页对应一个路由组件。

    <template>
      <div class="app-container">
        <bread-crumb />
    
        <!-- Tabs 页签 -->
        <el-tabs v-model="activeTab" @tab-click="handleTabClick">
          <el-tab-pane label="虚拟网卡" name="vnic"></el-tab-pane>
          <el-tab-pane label="其他页面" name="other"></el-tab-pane>
        </el-tabs>
    
        <!-- 根据 activeTab 渲染对应的组件 -->
        <component :is="currentComponent" />
      </div>
    </template>
    

    重点说明:

    • 使用 v-model="activeTab" 来绑定当前激活的 tab。
    • 使用 @tab-click="handleTabClick" 来监听 tab 切换事件。
    • 使用 <component :is="currentComponent" /> 动态渲染不同的页面组件。

    2. 定义组件映射关系

    data() 中定义一个对象,将 tab 名称映射到对应的组件路径。

    export default {
      data() {
        return {
          activeTab: 'vnic', // 默认激活的 tab
          componentMap: {
            vnic: () => import('@/views/vnic/index.vue'),
            other: () => import('@/views/other/index.vue')
          },
          currentComponent: null
        }
      },
      mounted() {
        this.loadComponent(this.activeTab)
      },
      methods: {
        loadComponent(name) {
          this.currentComponent = this.componentMap[name]
        },
        handleTabClick(tab) {
          this.loadComponent(tab.name)
        }
      }
    }
    

    重点说明:

    • 使用 import() 动态加载组件,提升性能。
    • mounted 中初始化默认组件。
    • handleTabClick 方法用于切换 tab 时加载对应的组件。

    3. 结合面包屑进行跳转(可选)

    如果你希望根据面包屑中的链接也能够切换 tabs,可以修改面包屑组件的 handleLink 方法,使其不仅跳转路由,还能更新 activeTab

    handleLink(item) {
      const { redirect, path } = item
      if (redirect) {
        this.$router.push(redirect)
      } else {
        this.$router.push(path)
      }
    
      // 如果是特定页面,更新 activeTab
      if (path.includes('vnic')) {
        this.activeTab = 'vnic'
      } else if (path.includes('other')) {
        this.activeTab = 'other'
      }
    }
    

    重点说明:

    • 面包屑点击后,除了跳转路由外,还更新 activeTab
    • 可以根据实际路由路径灵活判断要切换的 tab。

    4. 样式优化(可选)

    如果需要让 Tabs 与面包屑风格一致,可以在 CSS 中调整样式:

    .el-tabs {
      margin-top: 10px;
    }
    
    .el-tabs__item {
      font-size: 14px;
      color: #515f77;
    }
    

    ✅ 总结

    以下是完整的解决方案步骤:

    1. 在页面中引入 el-tabsel-tab-pane,用于展示多个 tab 页面。
    2. 动态加载组件,通过 component 标签实现不同 tab 的内容切换。
    3. 绑定 activeTab,控制当前激活的 tab。
    4. 在面包屑中处理点击事件,同步更新 activeTab
    5. 优化样式,使 Tabs 与整体页面风格统一。

    ✅ 示例代码(完整页面)

    <template>
      <div class="app-container">
        <bread-crumb />
    
        <el-tabs v-model="activeTab" @tab-click="handleTabClick">
          <el-tab-pane label="虚拟网卡" name="vnic"></el-tab-pane>
          <el-tab-pane label="其他页面" name="other"></el-tab-pane>
        </el-tabs>
    
        <component :is="currentComponent" />
      </div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          activeTab: 'vnic',
          componentMap: {
            vnic: () => import('@/views/vnic/index.vue'),
            other: () => import('@/views/other/index.vue')
          },
          currentComponent: null
        }
      },
      mounted() {
        this.loadComponent(this.activeTab)
      },
      methods: {
        loadComponent(name) {
          this.currentComponent = this.componentMap[name]
        },
        handleTabClick(tab) {
          this.loadComponent(tab.name)
        }
      }
    }
    </script>
    
    <style scoped>
    .el-tabs {
      margin-top: 10px;
    }
    </style>
    

    ✅ 注意事项

    • 确保你已经正确配置了 Vue Router,并且 vnicother 页面已注册为路由组件。
    • 若页面较多,建议使用 vue-routerlazy loading 加载方式,避免首屏加载过大。
    • 如果你需要在 tab 内部再次嵌套子路由,也可以使用 router-view 嵌套。

    如有更多需求,比如 保持 tab 内容状态动态生成 tabs 等,也可以继续扩展功能。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 8月12日
  • 已采纳回答 8月4日
  • 创建了问题 7月23日