| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 |
Tags
- 시멘틱태그
- useState
- Chart
- CSS
- npm
- type
- Package
- 프론트엔드개발
- NeXT
- vue-cli-service
- Download
- TradingVIew
- 프론트엔드
- error
- form
- axios
- react
- vscode
- 리액트기초
- components
- develop
- JavaScript
- antd
- 리액트
- FRONT
- 이미지
- frontend
- VUE
- err
- 상태관리
Archives
- Today
- Total
개발쬬
[Vue] 모바일/웹 분기설정 정규식예제 본문
반응형
환경 : Vue2 + vue-router@3.5.4 + vuex@3
main.js
const isMobileDevice =
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
navigator.userAgent
)
? true
: false;
//전역설정
Object.defineProperty(Vue.prototype, "$isMobileDevice", {
value: isMobileDevice,
});
//this.$isMobileDevice 로 활용이 가능하다
Test.vue
if (this.$isMobileDevice === true) {
url = res.data.mobile_url;
} else {
url = res.data.online_url;
}
또는 이렇게 router 설정에서 모바일 환경과 웹 환경설정 활용에 사용이 가능하다
router/index.js
import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "@/views/HomeView";
Vue.use(VueRouter);
let router = new VueRouter({
mode: "history",
base: process.env.BASE_URL,
routes: [
{
path: "/",
name: "HomeWeb",
component: () => import("@/views/web/HomeWeb"),
beforeEnter: (to, from, next) => {
let mobile =
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
navigator.userAgent
);
if (mobile === true) {
next("/m");
} else {
next();
}
},
meta: {
requiresAuth: false,
requiresAdmin: false,
},
},
{
path: "/m",
name: "HomeMobile",
component: () => import("@/views/web/HomeMobile"),
beforeEnter: (to, from, next) => {
let mobile =
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
navigator.userAgent
);
if (mobile !== true) {
next("/");
} else {
next();
}
},
meta: {
requiresAuth: false,
requiresAdmin: false,
},
}]
})
반응형
'쬬의 Vue' 카테고리의 다른 글
| [Vue] mycure/vue-wysiwyg 기본편집기 활용하기 (default 값 넣기) (0) | 2022.09.20 |
|---|---|
| [Vue] vue-cli-serve production 빌드 시 스타일 문제 해결과정 (0) | 2022.09.16 |
| [Vue] interceptors(인터셉터) 구현으로 axios 통신 공통으로 관리하기 (0) | 2022.08.17 |
| [Vue] vue-lottie 활용해 json 파일 로딩화면 구현하기 (0) | 2022.08.17 |
| [Vue] 앱과 웹뷰 통신 (0) | 2022.08.17 |