taro
taro copied to clipboard
路由中间件功能
这个特性解决了什么问题?
有一个全局登录信息的获取接口,要在进入页面前获取完成并存入storage,并在页面中使用,原来可以写在路由守卫之类的中间件中,请问现在应该写在哪里呢?
这个 API 长什么样?
类似路由中间件之类的功能
等待热心的小伙伴解决问题中..., 有一些相关的 issues 可能帮助到你!
Thank you so much!
加个 HOC 吧,好像没有更好的办法
给个参考方案:
/**
* 防止用户越过正常流程,例如说直接通过连接访问,或者尝试越权访问
* 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>
}
仅监听使用可以参考 #7470 使用 onAppRoute
完成小程序路由变化监听,h5 可以通过 __taroRouterChange
事件,如果需要拦截目前没有比较好的方法。
Duplicate of #7940