【Vue3/Vue2】判断设备是移动端还是pc端跳转不同路由router

文章讲述了如何在Vue3应用中,通过isMobile函数检测设备类型,并在onMounted钩子中根据路由路径和设备类型进行桌面与移动端的重定向。同时展示了在router文件中的配置和Vue2版本的实现方式。
摘要由CSDN通过智能技术生成

 

 

 

Vue3代码 

APP文件中写入js代码

1、首先,通过isMobile()函数判断用户的设备类型。该函数使用正则表达式匹配navigator.userAgent字符串,以确定用户是在移动设备上访问网页还是在桌面设备上访问网页

2、然后,在onMounted()钩子函数中,根据当前的路由路径来判断是否需要进行重定向。如果当前路径是根路径('/')或移动端首页路径('/mobile_index'),则会进一步检查设备类型。
3、如果是移动设备,则通过router.replace('/mobile_index')将路由重定向到移动端首页。如果是桌面设备,则通过router.replace('/')将路由重定向到桌面端首页。
<script setup lang="ts">
import { onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()
const 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;
}

onMounted(() => {
  if (route.path === '/' || route.path === '/mobile_index') {
    if (_isMobile()) {
      router.replace('/mobile_index')
    } else {
      router.replace('/')
    }
  }
})

router中写法

import type { RouteRecordRaw } from 'vue-router'
export const staticRoutes: Array<RouteRecordRaw> = [
  // pc
  {
    path: '/',
    component: () => import('../views/pc_index.vue'),
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () => import('../views/home/index.vue'),
        meta: {
          title: '首页',
          icon: 'ele-HomeFilled',
        },
      },
     
      // 公司产品
      {
        path: '/companyProducts',
        name: 'companyProducts',
        redirect: '/companyProducts/coalAR',
        children: [
          {
            path: '/companyProducts/coalAR',
            name: 'coalAR',
            component: () =>
              import('../views/companyProducts/coal/coalAR/index.vue'),
          }
          
        ],
      },
    
    ],
  },
  // 手机端首页
  {
    path: '/mobile_index',
    component: () => import('../views/mobile_index.vue'),
    redirect: '/mobileIndex',
    children: [
      {
        path: '/mobileIndex',
        name: 'mobileIndex',
        component: () => import('../views/mobile/mobileIndex.vue'),
        meta: {
          title: '首页',
          icon: 'ele-HomeFilled',
        }
      },
    
      // 公司产品
      {
        path: '/mobileProducts',
        name: 'mobileProducts',
        redirect: '/mobileProducts/coalAR',
        children: [
          {
            path: '/mobileProducts/coalAR',
            name: 'mobileCoalAR',
            component: () =>
              import('../views/mobile/mobileProducts/coal/coalAR/index.vue'),
          }
        ]
      }

    ]
  },
]

Vue2写法

在 App.vue 的 mounted 方法中对设置进行判断,如下:

//App.vue
mounted() {
    if (this._isMobile()) {
      alert("手机端");
      this.$router.replace('/m_index');
    } else {
      alert("pc端");
      this.$router.replace('/pc_index');
    }
  },
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;
}
}


/在 router/index.js 中有两个页面。

//在 router/index.js 中有两个页面。
export default new Router({
  mode: 'history',
  routes: [
    {
      path: '',
      redirect: '/pc_index'
    },
    {
      path: "/pc_index", // pc端首页
      name: PcIndex,
      component: PcIndex
    },
    {
      path: '/m_index', // 手机端首页
      name: MIndex,
      component: MIndex
    }
  ]
});

参考vue2链接地址:【Vue】判断设备是移动端还是pc端_vue判断是pc端还是移动端-CSDN博客

  • 14
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 3 中,你可以使用 Vue Router 进行路由跳转。下面是一些基本的步骤: 1. 首先,确保你已经安装了 Vue Router。你可以通过运行以下命令来安装它: ``` npm install vue-router@next ``` 2. 在你的主文件(例如 `main.js` 或 `main.ts`)中导入 Vue Router 和你的路由配置: ```javascript import { createApp } from 'vue'; import { createRouter, createWebHistory } from 'vue-router'; import App from './App.vue'; import Home from './views/Home.vue'; import About from './views/About.vue'; const routes = [ { path: '/', component: Home }, { path: '/about', component: About } ]; const router = createRouter({ history: createWebHistory(), routes }); const app = createApp(App); app.use(router); app.mount('#app'); ``` 在上面的示例中,我们创建了两个路由:`/` 对应 `Home` 组件,`/about` 对应 `About` 组件。你可以根据自己的需求添加更多的路由。 3. 在你的组件中使用路由跳转,你可以使用 `<router-link>` 组件或 `$router.push` 方法。 使用 `<router-link>` 组件: ```html <router-link to="/">Home</router-link> <router-link to="/about">About</router-link> ``` 使用 `$router.push` 方法: ```javascript // 在方法中调用 methods: { goToHome() { this.$router.push('/'); }, goToAbout() { this.$router.push('/about'); } } // 或者在模板中调用 <button @click="$router.push('/')">Go to Home</button> <button @click="$router.push('/about')">Go to About</button> ``` 这样,当用户点击链接或按钮时,路由就会进行跳转。 这只是一个基本的示例,你还可以使用更多的 Vue Router 功能,如路由参数、嵌套路由路由守卫等。你可以参考 Vue Router 的官方文档来了解更多信息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值