在app.vue頁面寫一個判斷事件并調(diào)用
_isMobile(){
? let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
? // localStorage.setItem('isiphone',flag)
? localStorage.setItem('ismobile',flag?1:0)
? return flag;
},
/ 判斷跳轉(zhuǎn)pc端頁面還是移動端頁面
// 使用鉤子函數(shù)對路由進(jìn)行權(quán)限跳轉(zhuǎn)
獲取存儲的ismobile 進(jìn)行判斷
navigator.userAgent 用來瀏覽器的user-agent信息
再用match()判斷是不是移動端
router.beforeEach((to, from, next) => {
var ismobile = localStorage.getItem('ismobile');
? if(ismobile == null){
? ? let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
? ? ismobile = flag ? 1 : 0
? }
? // 電腦
? if (ismobile == 0) {
? ? if (to.path == '/') {
? ? ? ? next('/admin_index')
? ? } else {
? ? ? ? next();
? ? }
? }
? // 手機(jī)
? if (ismobile == 1) {
? ? if (to.path == '/') {
? ? ? ? next('/home')
? ? } else {
? ? ? ? next();
? ? }
? }
})