Vue判断设备是移动端还是pc端
时间:2021-01-19 19:37:54
收藏:0
阅读:56
//在 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 } ]});在 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;
},
},
原文:https://www.cnblogs.com/skin999/p/14298973.html
评论(0)