Vue2在一个页面内动态切换菜单显示对应的路由组件

项目的需求是在一个页面内动态获取导航菜单,导航菜单切换的时候显示对应的路由页面,类似于tab切换的形式,切换的导航菜单和页面左侧导航菜单是同一个路由组件,只是放到了一个页面上,显示的个数不同,所有是动态获取的;效果如下图:

 使用动态加载路由方式import('@/views/pmc/info/index')import里面的是固定值,不能是变量,写变量就会报错,但我的需求是需要动态获取,所以找到了一个可行的方法,具体代码示例如下:

<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane v-for="(item,index) in list" :key="index" :label="item.dictName" :name="item.signName"></el-tab-pane>
      <!-- <el-tab-pane label="基本信息" name="second"></el-tab-pane>
      <el-tab-pane label="历史沿革" name="third"></el-tab-pane>-->
    </el-tabs>
    <keep-alive>  
      <!-- <component :is="currentComponent"></component> -->
      <component :is="asyncComponent" :mydeptId="deptId"></component>
    </keep-alive>
  </div>
</template>
<script>
import { menulist } from "@/api/pmc/MenuTab";

  export default {
    data() {
      return {
        activeName: 'second',//当前显示的tab的name
        currentComponent:"",// 当前组件的名字
        list:[],//菜单数组
        curdizhi:"",//当前菜单的路径,例如:"pmc/DeptBaseInfo/index"
        deptId:"",//公司的id,从路由地址传过来的
      };
    },
    components: { },
    computed: {  
      asyncComponent() {  
        // console.log("`@/views/${this.curdizhi}`--15:",`@/views/${this.curdizhi}`) 
        return () => require.ensure([], (require) => require(`@/views/${this.curdizhi}`))
      }  
    },
    created(){
      this.deptId=this.$route.query.deptId
      console.log("页面地址传来的参数:",this.deptId)
      //获取菜单列表
      this.getList();
      // this.currentComponent= () => import('@/views/pmc/info/index'); //第一个的组件地址路径,这里为了测试,应该写到getList()
      //方法的成功回调里面,当前组件currentComponent赋初值,还要给activeName赋初值
    },
    methods: {
      /** 查询菜单列表 */
      getList() {
        this.loading = true;
        menulist().then(response => {
          this.loading = false;
          console.log("菜单列表response3-13",response)
          this.list = response.rows;
          let oneobj=response.rows[0] //第一个路由对象
          // let one='@/views/'+response.rows[0].dictPath;//第一个数组中的对象路径
          //  that.currentComponent= () => import(one); //第一个的组件地址路径,这样写报错,import里面不能写变量
           this.activeName=oneobj.signName;//当前显示的标签的name赋值
           this.curdizhi=oneobj.dictPath //当前的路由页面
          
        });
      },
      //tab切换点击事件
      handleClick(tab, event) {
        // console.log("点击事件3-12:tab",tab);
        // console.log("点击事件3-12:event",event);
        console.log("点击的第几项index",tab.index)
        let index=tab.index;//tab选项在数组中的下标值
        let list=this.list;//菜单数组
        let curobj=list[index];//当前点击的路由对象
        let curdizhi=list[index].dictPath;//当前显示的菜单路由是点击的这条数据的路由地址
        this.curdizhi=curdizhi;//当前路由地址赋初值
        // if(index==0){
        //   this.currentComponent= () => import('@/views/pmc/info/index'); 
        // }
      }
    }
  };
</script>

相关推荐

  1. vue2 组件守卫使用

    2024-03-17 08:18:03       57 阅读
  2. Vue 实现组件切换

    2024-03-17 08:18:03       29 阅读
  3. Vue2 动态

    2024-03-17 08:18:03       32 阅读
  4. Vue Router 动态缓存组件

    2024-03-17 08:18:03       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-17 08:18:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-17 08:18:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-17 08:18:03       82 阅读
  4. Python语言-面向对象

    2024-03-17 08:18:03       91 阅读

热门阅读

  1. Oracle常用DBA相关语句

    2024-03-17 08:18:03       42 阅读
  2. Jenkins: 配置自动化发布脚本

    2024-03-17 08:18:03       46 阅读
  3. ISP相关

    ISP相关

    2024-03-17 08:18:03      42 阅读
  4. 2024.3.13-408学习笔记-C-数据在内存中的存储

    2024-03-17 08:18:03       39 阅读
  5. 前端小白的学习之路(CSS3 二)

    2024-03-17 08:18:03       47 阅读
  6. Vue基本用法

    2024-03-17 08:18:03       41 阅读
  7. 2024届 C++ 刷题 笔试强训 Day 02

    2024-03-17 08:18:03       43 阅读
  8. 请求头content-type的类型有什么?

    2024-03-17 08:18:03       42 阅读
  9. 发现数据之美:探索数据可视化的艺术与技术

    2024-03-17 08:18:03       40 阅读
  10. redis的基本知识点

    2024-03-17 08:18:03       37 阅读
  11. rust - windows窗口消息循环处理

    2024-03-17 08:18:03       41 阅读
  12. 测试人员如何进行需求分析?

    2024-03-17 08:18:03       45 阅读