判断是否为移动端

2021-12-08  本文已影响0人  萤火kin

判断是否为移动端

1、使用is-mobile依赖

"dependencies": {
    "is-mobile": "^2.2.1",
  },
import mobile from 'is-mobile'
export default [
  {
    path: '/account',
    name: 'account',
    component: Layout,
    redirect: '/account/web/personal/personal-info'
  },
  {
    path: '/account/web/personal/personal-info',
    name: 'personalAccount',
    component: () => import('@/views/account/web/personal/PersonalInfo'),
    meta: {
      accountInfo: true
    },
    beforeEnter: (to, from, next) => {
      if (store.state.account.tenantType === 'PERSON') {
        mobile() ? next({ name: 'mobileAccount' }) : next()
      } else {
        mobile()
          ? next({ name: 'mobileAccount' })
          : next({
              name: 'companyCompanyAccount'
            })
      }
    }
  },
}

2、利用util的方法判断

//  判断是否为移动端
export function hasMobile () {
    let isMobile = false;
    if((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
        // console.log('移动端');
        isMobile = true;
    }
    if (document.body.clientWidth < 800) {
        isMobile = true;
    }
    return isMobile
}
import {
    hasMobile
} from '@/libs/util';
export default {
    state: {
        hasMobile: hasMobile(),
    },
    mutations: {
        setMobile (state, hasMobile) {
            state.hasMobile = hasMobile
        },
    },
}

import { hasMobile } from '@/libs/util'

mounted () {
         // 浏览器的窗口发生改变时,才会触发resize函数)
        window.addEventListener('resize', debounce(() => {
            this.$store.commit('setMobile', hasMobile());
        }));
    }
@media screen and (min-width: 800px) {

}
上一篇下一篇

猜你喜欢

热点阅读