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

uniapp-route-guards

v1.0.7

Published

一个简单而又不失优雅的uniapp路由守卫

Downloads

26

Readme

route-guards

介绍

uniapp 中模拟并实现对应 vue-router 的部分 Api 的功能

平台差异说明

| App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 头条小程序 | QQ 小程序 | 360 小程序 | | ------- | --- | ---------- | ------------ | ---------- | ---------- | --------- | ---------- | | Android | √ | √ | √ | √ | √ | √ | √ |

如何安装

您可以使用 Yarn 或 npm 安装该软件包(选择一个)

yarn

yarn add uniapp-route-guards

npm

npm install uniapp-route-guards --save

插件注册

// main.js

import Vue from 'vue';
import UniRouteGuards from 'uniapp-route-guards';

Vue.use(UniRouteGuards);

全局前置守卫

// main.js

const guard = new UniRouteGuards();

// 自定义路由拦截白名单
const WHILE_LIST = ['/src/pages/home', '/src/pages/log'];

// 跳过路由白名单拦截
guard.beforeEach((to, from, next) => {
    if (WHILE_LIST.includes(from.url)) {
        return next(to.url);
    }

    next();
});

// 拦截 调用 uni.switchTab 页面C并跳转到 页面D
guard.beforeEach((to, from, next) => {
    console.log('\n');
    console.log('=================');
    console.log('guard.beforeEach');
    console.log('to: ', to);
    console.log('from: ', from);
    console.log('=================');
    console.log('\n');

    if (to.action === 'switchTab' && to.url === '/src/pages/c') {
        return next({
            url: '/src/pages/d',
            action: 'navigateTo'
        });
    }

    next();
});

全局后置后卫

guard.afterEach((to, from) => {
    console.log('\n');
    console.log('=================');
    console.log('guard.afterEach');
    console.log('to: ', to);
    console.log('from: ', from);
    console.log('=================');
    console.log('\n');
});

路由运行出时调用的守卫

// 注册 路由守卫出现异常回调的钩子
guard.onError((errMsg) => {
    console.error('route-guards error: ' + errMsg);
});

如何跳转路由并触发注册的守卫

路由跳转的使用方法和 uniapp 路由跳转一样的

// 例如
uni.navigateTo({
    url: '/pages/a'
});
uni.redirectTo({
    url: '/pages/a'
});
uni.reLaunch({
    url: '/pages/a'
});
uni.switchTab({
    url: '/pages/a'
});
uni.navigateBack();

取消对某个路由方法进行拦截

例如调用某个路由方法时并取消路由拦截

uni.navigateTo(
    {
        url: '/pages/a',
        success() {
            console.log('is success');
        },
        fail() {
            console.error('is fail');
        }
    },
    false
);

// 或
uni.navigateBack(null, false);

解析运行流程

  • 调用全局的 beforeEach 守卫
  • 路由跳转
  • 调用全局的 afterEach 守卫

暂时不支持的操作

1.在拦截器中访问 vm

// 例如:
guard.beforeEach((to, from, next) => {
    next((vm) => {
        // 通过 `vm` 访问组件实例
    });
});

2.拦截原生的 tabbar

3.拦截原生的 navBarback

Api

Event

| 参数名称 | 类型 | 默认值 | 是否必填 | 说明 | | ---------- | -------- | ------ | -------- | -------------------------------------------------- | | beforeEach | Function | - | false | 注册一个回调,在路由跳转之前运行 | | afterEach | Function | - | false | 注册一个回调,在路由跳转之后运行 | | onError | Function | - | false | 注册一个回调,该回调会在路由导航过程中出错时被调用 |

预览