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

savage-mini-router

v1.0.6

Published

微信小程序路由器,轻量,优雅,高效

Downloads

53

Readme

mini-router

微信小程序路由器,轻量,优雅,高效

安装

npm i @savage181855/mini-router -S

功能

  • 传递参数不需要序列化
  • 提供导航守卫进行路由拦截
  • 跳转和返回会进行页面栈的判断,优先返回页面栈里面存在的页面

使用说明

这里提供了两种路由器PlainRouterAdvanceRouter

准备工作

新建indexlogother等三个页面,并且在app.json里面把logother页面配置为tabBar页面

AdvanceRouter

新建router.js

import { AdvanceRouter, RouteConfigRaw } from "@savage181855/mini-router";

// 需要维护一个路由记录
const routes: RouteConfigRaw[] = [
  {
    name: "index",
    path: "/pages/index/index",
  },
  {
    name: "log",
    path: "/pages/log/log",
    type: "tab",
  },
  {
    name: "other",
    type: "tab",
    path: "/pages/other/other",
  },
];

export const router = new AdvanceRouter(routes);
router.beforeEnter((from, to, next) => {
  next();
});

router.afterEnter((from, to) => {
  console.debug(from, to, "afterEnter");
});

router.onRouteSuccess(function (res) {
  console.debug(res);
});

router.onRouteFail(function (res) {
  console.debug(res);
});

app.js中注入路由器

import { router } from "./router/index";

// 这样子可以在页面或组件中直接通过 this.$router 访问路由器
// 注意:这个方法必须在最顶部进行调用
router.inject();

App<IAppOption>({
  globalData: {},
  onLaunch() {
    console.debug(this);
  },
});

修改index.js

import { router } from "../../router/index";

Page({
  onLoad() {
    setTimeout(() => {
      router
        .push({ name: "other", params: "i am params" })
        .then((res) => {
          console.debug(res);
        })
        .catch((err) => {
          console.debug(err);
        });

      // 或者使用这种方式
      // this.$router
      //   .push({ name: "other", params: "i am params" })
      //   .then((res) => {
      //     console.debug(res);
      //   })
      //   .catch((err) => {
      //     console.debug(err);
      //   });
    }, 2000);
  },
});

修改other.js

import { router } from "../../router/index";

Page({
  onLoad() {
    // 获取上个页面传递的数据
    console.debug(router.getParams());
    // 或者使用这种方式
    // console.debug(this.$router.getParams());
  },
});

API 说明

PlainRouter

import { PlainRouter, RouteConfigRaw } from "@savage181855/mini-router";

export const router = new PlainRouter();
router.beforeEnter((from, to, next) => {
  next();
});

router.afterEnter((from, to) => {
  console.debug(from, to, "afterEnter");
});

router.onRouteSuccess(function (res) {
  console.debug(res);
});

router.onRouteFail(function (res) {
  console.debug(res);
});

app.js中注入路由器

import { router } from "./router/index";

// 这样子可以在页面或组件中直接通过 this.$router 访问路由器
// 注意:这个方法必须在最顶部进行调用
router.inject();

App<IAppOption>({
  globalData: {},
  onLaunch() {
    console.debug(this);
  },
});

修改index.js

import { router } from "../../router/index";

Page({
  onLoad() {
    setTimeout(() => {
      // path 参数必须跟 app.json 配置的一样
      router
        .push({ path: "/pages/log/log", params: "i am params" })
        .then((res) => {
          console.debug(res);
        })
        .catch((err) => {
          console.debug(err);
        });

      // 或者使用这种方式
      // this.$router
      //   .push({ path: "/pages/log/log", params: "i am params" })
      //   .then((res) => {
      //     console.debug(res);
      //   })
      //   .catch((err) => {
      //     console.debug(err);
      //   });
    }, 2000);
  },
});

修改other.js

import { router } from "../../router/index";

Page({
  onLoad() {
    // 获取上个页面传递的数据
    console.debug(router.getParams());
    // 或者使用这种方式
    // console.debug(this.$router.getParams());
  },
});

方法

  • beforeEnter() 前置守卫
router.afterEnter((from, to) => {
  console.debug(from, to, "afterEnter");
});
  • afterEnter() 后置守卫
  • navigateTo
  • switchTab
  • redirectTo
  • navigateBack
  • reLaunch
  • onRouteSuccess 监听路由导航成功
  • onRouteFail 监听路由导航成功
  • getCurrPage 获取当前页面
  • getPrevPage 获取上一个页面
  • getParams 获取页面传递的参数
  • inject 注入路由,可以从 this.$router 访问路由

AdvanceRouter

  • beforeEnter() 前置守卫
router.afterEnter((from, to) => {
  console.debug(from, to, "afterEnter");
});
  • afterEnter() 后置守卫
  • push 代替 switchTab 和 navigateTo
  • replace 代替 redirectTo
  • back 代替 navigateBack
  • reLaunch
  • onRouteSuccess 监听路由导航成功
  • onRouteFail 监听路由导航成功
  • getCurrPage 获取当前页面
  • getPrevPage 获取上一个页面
  • getParams 获取页面传递的参数
  • inject 注入路由,可以从 this.$router 访问路由