vue nuxt判断是pc端还是移动端,分别跳转不同页面

vue nuxt判断是pc端还是移动端,分别跳转不同页面

版本信息:
版本信息

一、defaule.vue,mounted()中进行判断:

if (this.isMobile()) {
  console.log("手机端");
  location.replace('xxxx');
} else {
  console.log("pc端");
}

methods中:

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)
	 return flag;
}

有问题,跳转时间比较长;有出现加载两次的现象。

二、使用 middleware中间件

  1. 在 middleware 下新建 midd.js
export default function ({ isServer, req, redirect, route }) {
    let pcOrigin = 'xxxx'
    let mobileOrigin = 'xxxx'
    let isMobile = (ua) => {
      return !!ua.match(/AppleWebKit.*Mobile.*/)
    }
    let userAgent = req ? req.headers['user-agent'] : navigator.userAgent || ''
    console.log(isMobile(userAgent))
    return isMobile(userAgent) ? redirect(mobileOrigin + route.fullPath) : redirect(pcOrigin + route.fullPath)
    // 使用redirect 重定向到外链需要加上前缀:http / https
  }
  1. 在 nuxt.config.js加上对应配置
router:{
    middleware: 'midd'
  },

参考地址:https://www.wandouip.com/t5i380032/

2020.08.11更新:
在本地是正常的,但是放在线上会出现死循环,所以采用了第三种方式
在这里插入图片描述

  1. 新建一个isMobile.js文件,路径自选,我的路径是/static/js/isMobile.js
(function() {
    console.log("判断移动端");
    var sUserAgent = navigator.userAgent.toLowerCase();
    if (/ipad|iphone|midp|rv:1.2.3.4|ucweb|android|windows ce|windows mobile/.test(sUserAgent)) {
        console.log("移动端");
        //跳转移动端页面
        window.location.replace("xxxx");//跳转后没有后退功能 
    } else {
        console.log("PC端");
    }
})();
  1. 在nuxt.config.js加上对应配置
head: {
    script:[  
      {src:'js/isMobile.js'},
    ]
  },
  1. 重新启动项目可以看到效果
Nuxt.js 中,我们可以使用插件判断是 PC 端还是移动端,然后根据不同的设备跳转不同的路由。 下面的代码演示了如何编写一个插件来实现这个功能: ```javascript // plugins/device.js export default ({ app }, inject) => { const userAgent = process.server ? app.$ua.get('User-Agent') : navigator.userAgent const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test( userAgent ) inject('device', { isMobile, redirect(path) { const url = isMobile ? `/m${path}` : path if (process.client) { window.location.href = url } else { app.context.redirect(url) } } }) } ``` 在插件中,我们首先获取了用户代理(userAgent),然后使用正则表达式来判断是否是移动设备。如果是移动设备,我们就将路径前缀设置为 "/m",否则就保持原样。 接着,我们定义了一个名为 "redirect" 的方法,用于根据设备类型跳转路由。如果在客户端中调用该方法,我们就直接跳转到指定的 URL;如果在服务器端中调用该方法,我们就使用上下文对象(context)的 redirect 方法来跳转路由。 最后,我们将插件作为 Nuxt.js 的插件进行注册: ```javascript // nuxt.config.js export default { plugins: ['~/plugins/device.js'] } ``` 现在,我们就可以在页面中使用该插件了。例如,我们可以编写一个页面组件来测试: ```html <!-- pages/index.vue --> <template> <div> <h1>首页</h1> <button @click="goToAbout">跳转到关于页</button> </div> </template> <script> export default { methods: { goToAbout() { this.$device.redirect('/about') } } } </script> ``` 在上面的代码中,我们定义了一个按钮,当用户点击该按钮时,就会调用 $device.redirect 方法来跳转到 "/about" 路由。如果是在移动设备上访问,就会跳转到 "/m/about" 路由。
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值