uniapp路由守衛


項目地址:https://hhyang.cn/v2/start/quickstart.html

​ 按照他的方法安裝,創建相應的js即可,有點基礎的自己搗鼓一下就可以了。我的應用場景是:沒有登錄痕跡->直接顯示登錄頁面;登錄痕跡存在則檢查是否在登錄狀態->在則不處理,不在則刪除痕跡。重點的來了!!下次啟動時直接啟動到登錄頁面。

需要注意的點:

  1. 啟動頁為一張空白頁面,且必須為非tabBar頁面
  2. 按照插件提示配置好相關參數,基本復制粘貼即可

開始配置:

//pages.json
"pages": [
	{
		"path":"pages/start/start"
	},
	{
		"path": "pages/login/index",
		"style": {
			"navigationBarTitleText": "登錄"
		}
	},
	{
		"path": "pages/index/index",
		"style": {
			"navigationBarTitleText": "首頁"
		}
	},
  }
]

<!-- start.vue -->
<template>
	<view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			
		},
		onShow() {
			if(!uni.getStorageSync('Session')){
				uni.redirectTo({
					url:'/pages/login/index'
				})
			}else{
				uni.switchTab({
					url:'/pages/index/index'
				})
			}
		}
	}
</script>

<style>
</style>
//router.js
router.beforeEach((to, from, next) => {
	//檢查是否存在Session,且不是來自login
	if(!uni.getStorageSync('Session') && to.path!=='/pages/login/index'){
		//沒有登錄,next到登錄頁面
        //這里一定要對to.path做判斷,否則會無限循環
		return next({path:'/pages/login/index',NAVTYPE :'replace'})
	}
	if(to.path=='/pages/start/start'){
        //如果有session,且地址是strt,證明正在啟動APP
		return next('/pages/index/index')
	}
  //其他路由不管
  next()
})

至此結束,完全OK


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM