文章目录
- 一、使用步骤
- 1.创建鉴权组件auth
- 2.app.json中注册全局组件
- 3.页面使用组件
- 4. 读取本地存储的 `token` 数据,用于判断是否曾登录过
- 5. 检测登录状态,要求未登录时不显示页面中的内容且跳转到登录页面
一、使用步骤
1.创建鉴权组件auth

2.app.json中注册全局组件

3.页面使用组件

4. 读取本地存储的 token
数据,用于判断是否曾登录过
App({
onLaunch() {
this.getToken()
},
getToken() {
this.token = wx.getStorageSync('token')
}
})
5. 检测登录状态,要求未登录时不显示页面中的内容且跳转到登录页面
<slot wx:if="{{isLogin}}"></slot>
Component({
data: {
isLogin: false,
},
lifetimes: {
attached() {
const isLogin = !!getApp().token
this.setData({ isLogin })
if (!isLogin) {
wx.redirectTo({
url: '/pages/login/index',
})
}
},
},
})