vue 判断移动端、PC端设备

615 阅读1分钟

/router/index.js

router.beforeEach((to, from, next) => {
  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
    )
  ) {
    if (to.path.indexOf("/p") != -1) {
      next({ path: to.path.replace("/p", "/m") });
    }
  } else {
    //PC端
    if (to.path.indexOf("/m") != -1) {
      next({ path: to.path.replace("/m", "/p") });
    }
  }
  next();
});