vue2、vue3+uni-app项目对pages.json文件拆分、动态生成路由管理、分包配置管理

本文介绍了如何通过JavaScript动态生成pages.json文件,包括依据模块划分不同页面配置、router/index.js和build.js的作用,以及如何使用package.json和npm命令进行测试和构建。
摘要由CSDN通过智能技术生成

1.动态生成pages.json文件准备工作:

        

  •  
    common/router/modules/*: 依据模块划分不同页面配置js文件
    
    common/router/index.js: 对应pages.js中除了页面pages配置的参数外,其他在pages.js文件中配置的参数都放这里面
    
    common/router/build.js:核心文件,主要是读取modules/目录下的文件以及router/index.js文件然后动态生成pages.json文件
    
    

    2.依据模块划分不同页面配置js文件

  • index.js 
  • user.js
  • process.js

3.index.js(配置pages.json)

4.build.js(动态生成pages.json文件)

const fs = require('fs')
const router = require('./index.js')
const home = require('./modules/index.js')
const user = require('./modules/user.js')
const process = require('./modules/process.js')

// 路由顺序
const allPageSetting = [
    home,
    user,
]

//加载子路由配置文件
const getRouter = () => {
    let router = []
    allPageSetting.forEach(item => {
        router = [...router, ...buildRouter(item)]
    })
    return router
}


//分包路由
const subPackagesPages = [
  process,
]

//加载需要分包的路由配置文件
const getSubPackagesRouter=()=>{
  let router = []
  subPackagesPages.forEach(item => {
      router = [...router, subPackagesBuildRouter(item)]
  })
  return router
}



// 构建分包页面路由
const subPackagesBuildRouter=(route)=>{
  const {baseUrl, children} = route
  const jo={
      root:baseUrl,
      pages:[]
  }
  children.forEach(item => {
      let obj = {};
      item.path ? obj.path = item.path : null;
      item.name ? obj.name = item.name : null;
      obj = {
          ...obj,
          style: {
              navigationBarTitleText: item.text
          }
      }
      item.style ? obj.style = item.name : null;
      jo.pages.push(obj)
  })

  return jo
}



//转化为pages.json文件格式
const buildRouter = route => {
    const res = []
    const {baseUrl, children} = route
    children.forEach(item => {
        let obj = {};
        item.path ? obj.path = baseUrl + item.path : null;
        item.name ? obj.name = item.name : null;
        obj = {
            ...obj,
            style: {
                navigationBarTitleText: item.text
            }
        }
        item.style ? obj.style = item.name : null;
        res.push(obj)
    })
    return res
}

//构建并写入 pages.json 文件
router.pages = getRouter()
router.subPackages = getSubPackagesRouter()
fs.rename('src/pages.json', 'src/pages.json.back', err => {
    if (err) {
        console.log(err)
    } else {
        console.log("pages.json文件备份成功!")
        fs.writeFile('src/pages.json',
            JSON.stringify(router, null, '  '),
            err => {
                err ? console.error(err) : console.log('pages.json文件更新成功!')
            }
        )
    }
})

5.配置package.json

6.执行测试

npm run serve  运行项目并动态生成pages.json

在已经运行的项目   npm run build:router   动态生成pages.json

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值