1.0.0 • Published 1 year ago

@syyfe/mini-router v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

mini-router

🚦 精巧强大的通用小程序路由,可以适配微信原生、Taro、Uniapp等。

使用

安装

npm i @syyfe/mini-router

typescript 引入

import Router, { RouteConfig } from "@syyfe/mini-router";

// 定义路由配置,isTab表示使用切换tab
const routeConfigList: RouteConfig[] = [
    { name: "test-tabbar", path: "pages/tabbar/test-tabbar/index", isTab: true },
    { name: "testA", path: "test/pages/testA/index" },
    { name: "testB", path: "test/pages/testB/index" },
    { name: "product-details", path: "test/pages/product-details/index" }
];

// 实例化,adapterInstance可以使用Taro,uniapp等
const router = new Router({ routes: allRoutes, adapterInstance: wx });

// 注册全局 beforeEach 钩子;使用方式和 vue-router 的 beforeEach 基本一致
router.beforeEach((to, from, next) => {
    console.log("当前路由", from);
    console.log("即将前往的路由", to);
    next();
    // next({ name: "pageB" });
    // next(false)
});

// 注册全局 afterEach 钩子
router.afterEach((current, from) => {
    console.log("跳转成功,当前路由:", current);
    console.log("之前路由:", from);
});

// 调用路由方法
router.push({ name: "testA" });

// 调用路由传参数
router.push({ name: "product-details", query: { id: "sb" } });