npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cogen-taro-router

v0.11.5

Published

让你的 Taro 路由跳转拥有类似 umi 框架约定式路由的体验。

Downloads

7

Readme

cogen-taro-router

让你的 Taro 路由跳转拥有类似 umi 框架约定式路由的体验。

简介

Taro 中内置了路由功能,这极大的方便了开发者。我们开发一个小程序页面时,流程是这样的。

  • 新建页面文件
  • app.config.ts 配置页面路径

使用时,需要在方法里书写完整页面路径。

navigateTo({ url: '/package-test/pages/test/index?id=1' })

比较好的开发方式应当是将所有页面路径维护到一个单独的路径映射表中。

navigateTo({ url: `${URLs.Test}?id=1` })

但这样的话,虽然方便了调用,但又需要多维护一份路由映射表文件。

因而这个脚本诞生了。

作用

  • 自动配置 app.config.ts 文件进行页面注册
  • 自动配置 project.config.json 文件,添加开发者工具页面编译快捷入口
  • 自动生成 routerService 文件,使得路由调用跳转更便捷。

环境

taro typescript

支持 react, vue

效果

效果图

安装

npm i cogen-taro-router -D

使用

经过配置后,使用非常简单,只需要新建页面文件,即可直接进行调用。

<View onClick={() => routerService.toTest({ id: 1 })}>跳转</View>

注意: 嫌配置麻烦的,可直接拷贝 example 代码进行使用

配置

注册插件

1、在根目录新建 generated 配置文件 .cogenrc.ts

2、注册插件

import { CogenConfig } from '@cogen/cli'

const cogenrc: CogenConfig = {
  configDir: './gconfig', // generated 插件配置目录
  plugins: [
    'cogen-taro-router', // 注册插件
  ],
}

export default cogenrc

配置插件

1、在根目录新建 gconfig 文件夹,文件夹下新建 router.ts 配置文件.

2、写入配置

import { Config } from 'cogen-taro-router'

const basePath = process.cwd()

export const taroRouter: Config = {
  // 源码目录
  pageDir: basePath + '/src',

  // app.config 路径
  appConfigPath: basePath + '/src/app.config.ts',

  // project.config.json 路径
  projectConfigPath: basePath + '/project.config.json',

  // 输出文件名
  outputFileName: 'routerService',

  /**
   * 导入组件
   *
   * 输出的文件将导入方法
   * import { customNavigateTo } from '@/business/app'
   */
  navigateFnName: 'customNavigateTo', // 导入方法名
  navigateSpecifier: '@/business/app', // 方法导入标识符

  ext: ['tsx', 'vue'], // 文件扩展(默认tsx)
}

注意:

  • 这里 taroRouter 名称不能变
  • 配置的函数(navigateFnName)需要满足传参为 customNavigateTo(pagePath: string, data: Object, opt: Object)

配置命令

1、在 package.json 中写入生成命令

  "scripts": {
    "gen": "generated",
  }

2、运行命令即可生成 routerService.ts 文件

npm run gen

路由方法

工具内部没有直接使用 taro 原生的 navigateTo 方法,而是需要手动配置方法。一是因为 taro 导出的路由 API 并不好用,二是 API 封装在内部,自定义程度不够高。您可以在保证函数传参条件的情况下,随意进行函数实现

import { navigateTo } from '@tarojs/taro'

export function customNavigateTo(pagePath: string, data?: any, opt?: any) {
  const url = createFinalURL(pagePath, data)
  navigateTo({ url, ...opt })
}

案例