wangting
2024-04-08 8be3182372eb774dce8f9c97a0b51c03bda165c7
Source/ProjectWeb/src/router/avue-router.js
@@ -6,7 +6,7 @@
RouterPlugin.install = function (vue, option = {}) {
  this.$router = option.router;
  this.$store = option.store;
  this.$vue = new vue({ i18n: option.i18n });
  this.$vue = new vue({i18n: option.i18n});
  // 这个的作用是 为了检查出网页链接,因为本项目用到了 iframe
  function isURL(s) {
@@ -85,20 +85,20 @@
    },
    //动态路由
    // 路由是专门的一个接口获取
      /**
       * aMenu: 接受到的动态路由数据 menu的结构外层有父级path 里面有一个childen 记录页面的路由
       * first: 为了区分外界 调用formatRoutes 和 当前文件调用 formatRoutes
      */
    /**
     * aMenu: 接受到的动态路由数据 menu的结构外层有父级path 里面有一个childen 记录页面的路由
     * first: 为了区分外界 调用formatRoutes 和 当前文件调用 formatRoutes
     */
    formatRoutes: function (aMenu = [], first) {
      // window.console.log('aMenu')
      // window.console.log(aMenu)
      const aRouter = []
      // 获取到全局配置中的 props
      const propsConfig = this.$website.menu.props;
      // 设置 props默认值 作用就是将字段设置成配置的
      const propsDefault = {
        label: propsConfig.label || 'name',
        path: propsConfig.path || 'path',
        path:'code',
        code: 'code',
        pathValue:'pathValue',
        icon: propsConfig.icon || 'icon',
        children: propsConfig.children || 'children',
        meta: propsConfig.meta || 'meta',
@@ -111,20 +111,30 @@
        const oMenu = aMenu[i];
        // 这一块的赋值 也就是取到返回的值
        let path = (() => {
            if (first) {
              // 将 '/index' 替换为 ''
              return oMenu[propsDefault.path].replace('/index', '')
            } else {
              return oMenu[propsDefault.path]
            }
          })(),
          //特殊处理组件 执行完这个 component 也就是精确到具体的文件了  views文件夹下面就是具体的页面代码
          component = 'views' + oMenu.path,
          if (first) {
            // 将 '/index' 替换为 ''
            return oMenu[propsDefault.pathValue].replace('/index', '')
          } else {
            return oMenu[propsDefault.pathValue]
          }
        })();
        let code = (() => {
          return oMenu[propsDefault.code]
        })();
        // 将字符串分割成数组,以'?'作为分隔符
        let parts = path.split("?");
        // 如果数组的长度大于1,表示有'?',则取第一个元素的第一个部分,否则直接取整个字符串
        let bsValue = parts.length > 1 ? parts[0].split("=")[1] : path.split("=")[1];
        if (bsValue === '' || bsValue === undefined || bsValue === null) {
          bsValue = "UI";
        }
        //特殊处理组件 执行完这个 component 也就是精确到具体的文件了  views文件夹下面就是具体的页面代码
        let component = bsValue === 'UI' ? 'views/base/UIContentViewer' : `views/custom-ui/${bsValue}`,
          name = oMenu[propsDefault.label],
          icon = oMenu[propsDefault.icon],
          children = oMenu[propsDefault.children],
          meta = oMenu[propsDefault.meta] || {};
          // meta中 keepalive 的处理
        // meta中 keepalive 的处理
        meta = Object.assign(meta, (function () {
          if (option.keepAlive === true) {
            return {
@@ -135,7 +145,7 @@
        //是否有子路由
        const isChild = children.length !== 0;
        const oRouter = {
          path: path,
          path: '/' + code,
          component(resolve) {
            // 判断是否为首路由
            if (first) {
@@ -163,7 +173,7 @@
          children: !isChild ? (() => {
            if (first) {
              // 这里的isURL判断,因为这个网站有使用 iframe。所以需要判断是否为网页链接
              if (!isURL(path)) oMenu[propsDefault.path] = `${path}/index`;
              if (!isURL(path)) oMenu[propsDefault.pathValue] = `${path}/index`;
              return [{
                component(resolve) {
                  require([`../${component}.vue`], resolve)
@@ -179,7 +189,7 @@
            /**
             * 这里是重点,当有子路由的时候 会再去执行 formatRoutes 方法,然后又会有一个新的 aMenu for循环。
             * 最后返回的是一个数组 aRouter 这个数组就会作为 childen的值被 return
            */
             */
            return this.formatRoutes(children, false)
          })()
        }