Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

路由中间件功能 #11378

Closed
lxsunbin opened this issue Mar 2, 2022 · 4 comments
Closed

路由中间件功能 #11378

lxsunbin opened this issue Mar 2, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@lxsunbin
Copy link

lxsunbin commented Mar 2, 2022

这个特性解决了什么问题?

有一个全局登录信息的获取接口,要在进入页面前获取完成并存入storage,并在页面中使用,原来可以写在路由守卫之类的中间件中,请问现在应该写在哪里呢?

这个 API 长什么样?

类似路由中间件之类的功能

@taro-bot2 taro-bot2 bot added the enhancement New feature or request label Mar 2, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2022

等待热心的小伙伴解决问题中..., 有一些相关的 issues 可能帮助到你!

Thank you so much!

@axetroy
Copy link

axetroy commented Jun 29, 2022

加个 HOC 吧,好像没有更好的办法

@jecyu
Copy link

jecyu commented Oct 13, 2022

给个参考方案:

/**
 * 防止用户越过正常流程,例如说直接通过连接访问,或者尝试越权访问
 * taro 目前不支持路由中间件,需开发者在需要鉴权的页面包装多一层
 * <Auth>
 *  <Order/>
 * </Auth>
 */

import { View } from "@tarojs/components";
import React, { useEffect, useState } from 'react';
import { appAuth } from "@/utils";

export default props => {
    const [hasAuth, setHasAuth] = useState(false);
    const { children, ...params } = props;
    useEffect(() => {
        appAuth(params).then(({ success }) => setHasAuth(success));
    }, []);
    return <View>{hasAuth ? children : 'noAuth '}</View>;
};

使用

function Order() { return <View><View>}
export default function AuthOrder() {
  return <Auth projectName='order'>
    <Order />;
  </Auth>
}

@ZakaryCode
Copy link
Contributor

仅监听使用可以参考 #7470 使用 onAppRoute 完成小程序路由变化监听,h5 可以通过 __taroRouterChange 事件,如果需要拦截目前没有比较好的方法。

Duplicate of #7940

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants