uniapp启动时判断登录,已登录跳转首页,未登录跳转登录页
uniapp启动时判断是否登录,如果登录跳转到首页,如果未登录跳转到登录页。
有两种实现方式。
方式一
- 在在manifest.json文件中配置启用启动页相关参数,如下图所示。
2. App启动时判断是否登录,如果未登录跳转到登录页面,已登录跳转到首页
// App.vue
<script>
export default {
onLaunch: function() {
console.log('App Launch')
// 方式一:1.在manifest.json文件中配置启用启动页,参考当前配置。2.APP启动时判断是否登录,如果未登录则跳转到登录界面并关闭启动页,如果已登录则关闭启动页,
let token = uni.getStorageSync('token')
if (token) {
// #ifdef APP-PLUS
plus.navigator.closeSplashscreen();
// #endif
} else {
// #ifdef APP-PLUS
uni.reLaunch({
url: '/pages/login/login',
success: () => {
plus.navigator.closeSplashscreen();
}
})
// #endif
// #ifndef APP-PLUS
uni.reLaunch({
url: '/pages/login/login',
})
// #endif
}
},
onShow: function() {
console.log('App Show')
},
onHide: function() {
console.log('App Hide')
}
}
</script>
<style>
/*每个页面公共css */
</style>
方式二
新建一个空白页面,将空白页面放在pages.json中pages的第一个位置,作为首页。然后空白页中判断是否登录,如果未登录跳转登录,如果登录跳转到登录后的首页
pages.json
preIndex.vue
<template>
<view>
我是一个空白的启动页面
</view>
</template>
<script setup>
import {
onLoad
}
from '@dcloudio/uni-app'
onload(() => {
let token = uni.getStorageSync('token')
console.log(token)
if (token) {
uni.reLaunch({
url: "/pages/index/index"
})
} else {
uni.reLaunch({
url: '/pages/login/login',
})
}
})
</script>
<style>
</style>
uniapp启动时判断登录,已登录跳转首页,未登录跳转登录页
https://www.diaoyc.cn//archives/1716136704881