文章目录
- 一、使用步骤
- 1.创建鉴权组件auth
- 2.app.json中注册全局组件
- 3.页面使用组件
- 4. 读取本地存储的 `token` 数据,用于判断是否曾登录过
- 5. 检测登录状态,要求未登录时不显示页面中的内容且跳转到登录页面
一、使用步骤
1.创建鉴权组件auth
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/a77043bd73b4431d9470efc2791d4475.png)
2.app.json中注册全局组件
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/05a79ce09c874288abf6d5801666c735.png)
3.页面使用组件
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/96867a2db9bf407ea28fd7894abf2ecc.png)
4. 读取本地存储的 token
数据,用于判断是否曾登录过
html" title=javascript>javascript">
App({onLaunch() {this.getToken()},getToken() {this.token = wx.getStorageSync('token')}
})
5. 检测登录状态,要求未登录时不显示页面中的内容且跳转到登录页面
<slot wx:if="{{isLogin}}"></slot>
html" title=javascript>javascript">
Component({data: {isLogin: false,},lifetimes: {attached() {const isLogin = !!getApp().tokenthis.setData({ isLogin })if (!isLogin) {wx.redirectTo({url: '/pages/login/index',})}},},
})