宅男在线永久免费观看网直播,亚洲欧洲日产国码无码久久99,野花社区在线观看视频,亚洲人交乣女bbw,一本一本久久a久久精品综合不卡

全部
常見問題
產(chǎn)品動(dòng)態(tài)
精選推薦

vue2.0根據(jù)菜單信息動(dòng)態(tài)添加路由

管理 管理 編輯 刪除

在開發(fā)的過程中,一般都是后端去做權(quán)限的設(shè)置和配置前端的路由,前端根據(jù)后臺(tái)編寫相應(yīng)的路由。這樣就很做到權(quán)限的控制。在某些情況,需要添加的路由不確定,需要從后端獲取數(shù)據(jù),并且后端更新相關(guān)的路由時(shí),頁面也能夠立即渲染出來,這時(shí)候就需要使用動(dòng)態(tài)路。

原來的路由目錄結(jié)構(gòu)

例如OA管理系統(tǒng)中,菜單中的很多路由都是不確定的,即使你寫了10個(gè)路由,但是后端那邊新增了10個(gè)路由,那么這時(shí)候設(shè)置動(dòng)態(tài)添加路由后,就可以自動(dòng)在第一時(shí)間創(chuàng)建出所有的路由,而不需要你手動(dòng)的寫了。

1、設(shè)置默認(rèn)路由

import Vue from 'vue';
import Router from 'vue-router';
import { roterPre } from '@/settings';
Vue.use(Router);
/* Layout */
import Layout from '@/layout';





import defaultRoutes from '@/router/routes';
import companyRouter from '@/router/company';
export const constantRoutes = [...defaultRoutes, companyRouter];



const createRouter = () =>
  new Router({
    mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();
// 解決刷新頁面后路由重置
getRouterMenus();

export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;

其中defaultRoutes ,companyRouter 為系統(tǒng)中的默認(rèn)菜單。

2、獲取系統(tǒng)模板以及處理后臺(tái)返回的菜單

// 獲取views中以.vue結(jié)尾的文件
const contextInfo = require.context('../views', true, /.vue$/);

/**
 * 過濾/components中的vue文件
 * @returns {{}}
 */
const filteredFileNames = () => {
  const files = {};
  contextInfo.keys().forEach((fileName) => {
    if (!fileName.includes('/components')) {
      const pathConfig = contextInfo(fileName);
      let path = '/' + fileName.substring(2, fileName.length - 4);
      files[path] = pathConfig.default;
    }
  });
  return files;
};
const files = filteredFileNames();

以上的處理就是為了的到views中以.vue結(jié)尾的組件,并且過濾掉components中的.vue文件。這一步主要是解決ES6 import()  中不能使用變量加載組件的問題。 files中的數(shù)據(jù)格式為

{
   "/uesr/calendar/index":components,
   "/uesr/duty/analyse":components,
   ...
}

3、處理后臺(tái)返回的菜單及生成路由

/**
 * 刪除不需要配置路由
 * @param items
 * @param paths
 */
const removeItems = (items, paths) => {
  for (let i = 0; i < items.length; i++) {
    const item = items[i];
    for (let j = 0; j < paths.length; j++) {
      const path = paths[j];
      if (item.menu_path === path) {
        items.splice(i, 1);
        i--;
        break;
      } else if (item.children) {
        removeItems(item.children, paths);
        if (item.children.length === 0) {
          delete item.children;
        }
      }
    }
  }
};

/**
 * 處理平臺(tái)菜單
 * @param menus
 * @param parentPath
 */
const replaceMenuPath = (menus, parentPath) => {
  menus.forEach((menu, index) => {
    if (menu.children) {
      replaceMenuPath(menu.children, menu.menu_path);
    }
    if (parentPath !== '/' && menu.menu_path.indexOf(parentPath) > -1) {
      menu.new_path = menu.menu_path.replace(parentPath + '/', '');
      menu.component = menu.menu_path.replace(roterPre, '');
    }
    if (menu.menu_path !== '/') {
      menu.component = menu.menu_path.replace(roterPre, '');
    }
  });
};

/**
 * 過濾路由所需要的數(shù)據(jù)
 * @param routes
 * @returns {*}
 */
const filterAsyncRoutes = (routes) => {
  return routes.map((route) => {
    const routeRecord = createRouteRecord(route);
    if (route.children != null && route.children && route.children.length) {
      routeRecord.children = filterAsyncRoutes(route.children);
    }
    return routeRecord;
  });
};

/**
 * 創(chuàng)建一條路由記錄
 * @param route
 * @returns {{path: (*|string), meta: {noCache: boolean, icon, title: *}, name: *}}
 */
const createRouteRecord = (route) => {
  const routeRecord = {
    path: route.pid === 0 ? route.menu_path : route.new_path ? route.new_path : '/',
    name: route.unique_auth,
    meta: {
      title: route.menu_name,
      icon: route.icon,
      noCache: true,
    },
  };

  if (route.pid === 0) {
    routeRecord.component = Layout;
  } else if (route.pid > 0 && route.children && route.children.length > 0) {
    // 解決父級不寫component 屬性,子集的component也不會(huì)顯示問題
    routeRecord.component = { render: (e) => e('router-view') };
  } else {
    routeRecord.component = files[route.component];
  }
  return routeRecord;
};

/**
 * 根據(jù)后臺(tái)菜單動(dòng)態(tài)生成路由
 */
export const getRouterMenus = () => {
  const entMenuList = JSON.parse(localStorage.getItem('entMenuList'));
  let entRouter = [];
  if (entMenuList && entMenuList.length > 0) {
    // 移除不需要處理路由
    removeItems(entMenuList, ['/admin/user/work']);
    // 處理后臺(tái)返回的路由結(jié)構(gòu)
    replaceMenuPath(entMenuList, '/');
    entRouter = filterAsyncRoutes(entMenuList);
    constantRoutes.push(...entRouter);
    // 路由重置加載
    resetRouter();
  }
};

其中constantRoutes為開始定義的數(shù)組,便于存儲(chǔ)路由。

4、最終index.js邏輯源碼

import Vue from 'vue';
import Router from 'vue-router';
import { roterPre } from '@/settings';
Vue.use(Router);
/* Layout */
import Layout from '@/layout';

// 獲取views中以.vue結(jié)尾的文件
const contextInfo = require.context('../views', true, /.vue$/);

/**
 * 過濾/components中的vue文件
 * @returns {{}}
 */
const filteredFileNames = () => {
  const files = {};
  contextInfo.keys().forEach((fileName) => {
    if (!fileName.includes('/components')) {
      const pathConfig = contextInfo(fileName);
      let path = '/' + fileName.substring(2, fileName.length - 4);
      files[path] = pathConfig.default;
    }
  });
  return files;
};
const files = filteredFileNames();

/**
 * 刪除不需要配置路由
 * @param items
 * @param paths
 */
const removeItems = (items, paths) => {
  for (let i = 0; i < items.length; i++) {
    const item = items[i];
    for (let j = 0; j < paths.length; j++) {
      const path = paths[j];
      if (item.menu_path === path) {
        items.splice(i, 1);
        i--;
        break;
      } else if (item.children) {
        removeItems(item.children, paths);
        if (item.children.length === 0) {
          delete item.children;
        }
      }
    }
  }
};

/**
 * 處理平臺(tái)菜單
 * @param menus
 * @param parentPath
 */
const replaceMenuPath = (menus, parentPath) => {
  menus.forEach((menu, index) => {
    if (menu.children) {
      replaceMenuPath(menu.children, menu.menu_path);
    }
    if (parentPath !== '/' && menu.menu_path.indexOf(parentPath) > -1) {
      menu.new_path = menu.menu_path.replace(parentPath + '/', '');
      menu.component = menu.menu_path.replace(roterPre, '');
    }
    if (menu.menu_path !== '/') {
      menu.component = menu.menu_path.replace(roterPre, '');
    }
  });
};

/**
 * 過濾路由所需要的數(shù)據(jù)
 * @param routes
 * @returns {*}
 */
const filterAsyncRoutes = (routes) => {
  return routes.map((route) => {
    const routeRecord = createRouteRecord(route);
    if (route.children != null && route.children && route.children.length) {
      routeRecord.children = filterAsyncRoutes(route.children);
    }
    return routeRecord;
  });
};

/**
 * 創(chuàng)建一條路由記錄
 * @param route
 * @returns {{path: (*|string), meta: {noCache: boolean, icon, title: *}, name: *}}
 */
const createRouteRecord = (route) => {
  const routeRecord = {
    path: route.pid === 0 ? route.menu_path : route.new_path ? route.new_path : '/',
    name: route.unique_auth,
    meta: {
      title: route.menu_name,
      icon: route.icon,
      noCache: true,
    },
  };

  if (route.pid === 0) {
    routeRecord.component = Layout;
  } else if (route.pid > 0 && route.children && route.children.length > 0) {
    // 解決父級不寫component 屬性,子集的component也不會(huì)顯示問題
    routeRecord.component = { render: (e) => e('router-view') };
  } else {
    routeRecord.component = files[route.component];
  }
  return routeRecord;
};

import defaultRoutes from '@/router/routes';
import companyRouter from '@/router/company';
export const constantRoutes = [...defaultRoutes, companyRouter];

/**
 * 根據(jù)后臺(tái)菜單動(dòng)態(tài)生成路由
 */
export const getRouterMenus = () => {
  const entMenuList = JSON.parse(localStorage.getItem('entMenuList'));
  let entRouter = [];
  if (entMenuList && entMenuList.length > 0) {
    // 移除不需要處理路由
    removeItems(entMenuList, ['/admin/user/work']);
    // 處理后臺(tái)返回的路由結(jié)構(gòu)
    replaceMenuPath(entMenuList, '/');
    entRouter = filterAsyncRoutes(entMenuList);
    constantRoutes.push(...entRouter);
    // 路由重置加載
    resetRouter();
  }
};

const createRouter = () =>
  new Router({
    mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();
// 解決刷新頁面后路由重置
getRouterMenus();

export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;

最終的目錄結(jié)構(gòu)。簡化了好多需要自己手動(dòng)創(chuàng)建的路由。


請登錄后查看

武湘君 最后編輯于2023-05-22 10:21:39

快捷回復(fù)
回復(fù)
回復(fù)
回復(fù)({{post_count}}) {{!is_user ? '我的回復(fù)' :'全部回復(fù)'}}
排序 默認(rèn)正序 回復(fù)倒序 點(diǎn)贊倒序

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level || item.bbs_level }}

作者 管理員 企業(yè)

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推薦': '推薦'}}
{{item.is_suggest == 1? '取消推薦': '推薦'}}
沙發(fā) 板凳 地板 {{item.floor}}#
{{item.user_info.title || '暫無簡介'}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
打賞
已打賞¥{{item.reward_price}}
{{item.like_count}}
{{item.showReply ? '取消回復(fù)' : '回復(fù)'}}
刪除
回復(fù)
回復(fù)

{{itemc.user_info.nickname}}

{{itemc.user_name}}

回復(fù) {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}
打賞
已打賞¥{{itemc.reward_price}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回復(fù)' : '回復(fù)'}}
刪除
回復(fù)
回復(fù)
查看更多
打賞
已打賞¥{{reward_price}}
4074
{{like_count}}
{{collect_count}}
添加回復(fù) ({{post_count}})

相關(guān)推薦

快速安全登錄

使用微信掃碼登錄
{{item.label}} 加精
{{item.label}} {{item.label}} 板塊推薦 常見問題 產(chǎn)品動(dòng)態(tài) 精選推薦 首頁頭條 首頁動(dòng)態(tài) 首頁推薦
取 消 確 定
回復(fù)
回復(fù)
問題:
問題自動(dòng)獲取的帖子內(nèi)容,不準(zhǔn)確時(shí)需要手動(dòng)修改. [獲取答案]
答案:
提交
bug 需求 取 消 確 定
打賞金額
當(dāng)前余額:¥{{rewardUserInfo.reward_price}}
{{item.price}}元
請輸入 0.1-{{reward_max_price}} 范圍內(nèi)的數(shù)值
打賞成功
¥{{price}}
完成 確認(rèn)打賞

微信登錄/注冊

切換手機(jī)號(hào)登錄

{{ bind_phone ? '綁定手機(jī)' : '手機(jī)登錄'}}

{{codeText}}
切換微信登錄/注冊
暫不綁定
CRMEB客服

CRMEB咨詢熱線 咨詢熱線

400-8888-794

微信掃碼咨詢

CRMEB開源商城下載 源碼下載 CRMEB幫助文檔 幫助文檔
返回頂部 返回頂部
CRMEB客服