uniapp 动态生成page.json,自动拆分页面及自动注册页面

自动生成文件,需要借助原生nodejs的能力,然后通过获取文件及文件名,进行数据生成,并写入到page.json里面

1,在你的uniapp项目中创建如上目录

2,small.js文件

javascript 复制代码
module.exports = {
    baseUrl: 'pages/small/',
    children: [
      {
        path: 'register',
        name: '注册'
      }, {
        path: 'login',
        name: '登录'
      }
    ]
  }
  

3,build.js文件

javascript 复制代码
const fs = require('fs')
const path = require('path')
const router = require('./index.js')


// 将子路由模块配置文件转化为 uniapp 配置文件格式
const buildRouter = route => {
  const res = []
  const { baseUrl, children } = route
  children.forEach(i => {
    const obj = {
      path: baseUrl + i.path,
      style: {
        navigationBarTitleText: i.name
      }
    }
    Object.keys(i).forEach(ii => {
      !['path', 'name'].includes(ii) && (obj.style[ii] = i[ii])
    })
    res.push(obj)
  })
  return res
}

// 自动加载 './modules' 目录子路由配置文件
const getRouter = () => {
  const srcPath = path.resolve(__dirname, './modules')
  const result = fs.readdirSync(srcPath)
  let router = []
  result.forEach(r => {
    const route = require('./modules/' + r)
    router = [...router, ...buildRouter(route)]
  })
  return router
}

// 构建 pages 并写入 pages.json 文件
router.pages = getRouter()
fs.writeFile(
  __dirname + '/../pages.json',
  // 我这边是用两个空格来缩进 pages.json,如果喜欢制表符,第三个参数更换你为 \t 即可
  JSON.stringify(router, null, '  '),
  e => e ? console.error(e) : console.log('pages.json 配置文件更新成功')
)

4,index.js文件

javascript 复制代码
module.exports = {
    globalStyle: {
      navigationBarTextStyle: 'white',
      navigationBarTitleText: '司机配送',
      navigationBarBackgroundColor: '#4a9ff8',
      backgroundColor: '#4a9ff8'
    },
    tabBar: {
      color: '#666',
      selectedColor: '#4a9ff8',
      backgroundColor: '#f7f7f7',
      borderStyle: 'white',
      list: [
        {
          pagePath: 'pages/index/index',
          // iconPath: 'static/images/icon-homeed.png',
          // selectedIconPath: 'static/images/icon-home.png',
          text: '首页'
        },
        {
          pagePath: 'pages/small/login',
          // iconPath: 'static/images/icon-tasked.png',
          // selectedIconPath: 'static/images/icon-task.png',
          text: '任务'
        },
        // {
        //   pagePath: 'pages/my/my',
        //   iconPath: 'static/images/icon-myed.png',
        //   selectedIconPath: 'static/images/icon-my.png',
        //   text: '我的'
        // }
      ]
    },
    condition: { //模式配置,仅开发期间生效
      current: 0, //当前激活的模式(list 的索引项)
      list: [{
        name: "test", //模式名称
        path: "pages/index/index" //启动页面,必选
      }]
    },
  }

上面就是所有的配置文件,通过index.js去配置通用配置,通过modules去吧页面进行分类,最后在build.js里面进行重构并生成所需的page.json文件

以上部分,其实实现了自主配置,但是其实有时候,我只是想偷懒,页面我都不想配置,所以我做出了如下改进:

主要还是设计build.js文件:

javascript 复制代码
const fs = require('fs')
const path = require('path')
const router = require('./index.js')
let data = []

// 获取所有页面,进行路由配置
const getPageRouter = (pagePath) =>{
  const srcPath = path.resolve(__dirname, pagePath)
  const result = fs.readdirSync(srcPath)
  let reg =/[\.]/

  result.forEach(val =>{
    if(reg.test(val)){
      data.push({
        // 页面配置不需要前面的路径
        path: (pagePath + val).slice(3,-4),
        style: {
          navigationBarTitleText: '页面'
        }
      })
    }else{
      getPageRouter(pagePath + val + '/')
    }
  })
  return data
}

// 构建 pages 并写入 pages.json 文件
router.pages = getPageRouter('../pages/')
fs.writeFile(
  __dirname + '/../pages.json',
  // 我这边是用两个空格来缩进 pages.json,如果喜欢制表符,第三个参数更换你为 \t 即可
  JSON.stringify(router, null, '  '),
  e => e ? console.error(e) : console.log('pages.json 配置文件更新成功')
)

上面通过getPageRouter进行页面自动读取,递归子文件夹,然后生成现在所使用的所有页面目录,统一放入page.json里面,实现页面自动注册,这里就不需要modules了,如果对页面有统一配置,像style,可以在data.push里面的那个对象里面进行并入,这里写的顶栏配置只是一个例子

相关推荐
im_AMBER35 分钟前
weather-app开发手记 02 JSON基础 | API 调用 400 错误修复 | JWT 认证问题
笔记·学习·json·axios·jwt
2501_915909061 小时前
手机崩溃日志导出的工程化体系,从系统级诊断到应用行为分析的多工具协同方法
android·ios·智能手机·小程序·uni-app·iphone·webview
郑州光合科技余经理2 小时前
技术视角:海外版一站式同城生活服务平台源码解析
java·开发语言·uni-app·php·排序算法·objective-c·生活
wangdaoyin20105 小时前
UniApp中使用LivePlayer进行视频或在流媒体播放
uni-app·liveplayer·h5播放视频
2501_915106325 小时前
App HTTPS 抓包实战解析,从代理调试到真实网络流量观察的完整抓包思路
网络协议·http·ios·小程序·https·uni-app·iphone
游戏开发爱好者86 小时前
苹果App Store应用程序上架方式全面指南
android·小程序·https·uni-app·iphone·webview
2501_916008896 小时前
深入理解 iPhone 文件管理,从沙盒结构到开发调试的多工具协同实践
android·ios·小程序·https·uni-app·iphone·webview
一室易安7 小时前
解决使用 UniApp 搭配 Vue3 小程序开始 使用uview-plus 的返回顶部up-back-top中onPageScroll 不触发的问题
小程序·uni-app
yilan_n7 小时前
鸿蒙应用上传
vue.js·华为·uni-app
yilan_n7 小时前
【UniApp实战】手撸面包屑导航与路由管理 (拒绝页面闪烁)
前端·javascript·vue.js·uni-app·gitcode