判断移动端还是PC端
router.js
1 routes: [ 2 { 3 name: 'pc', path: '/pc', component: pc, redirect: '/pc/homePage', meta: {title: 'pc端'}, 4 children: 5 [ 6 {name: 'homePage', path: 'HomePage', component: HomePage, meta: {title: '首页'}}, 7 ] 8 }, 9 {name: 'mobile', path: '/mobile', component: mobile,redirect: '/mobile/mobileHomePage', meta: {title: 'web首页'}, 10 children:[ 11 {name: 'mobileHomePage', path: 'mobileHomePage', component: mobileHomePage, meta: {title: '首页'}}, 12 ] 13 } 14 ]
app.vue
1 2 3 mounted() { 4 if (this._isMobile()) { 5 this.$router.replace('/mobile'); 6 } else { 7 this.$router.replace('/pc'); 8 } 9 }, 10 methods: { 11 _isMobile() { 12 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); 13 return flag; 14 } 15 }