Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display different Vuejs components for mobile browsers

Tags:

vue.js

vuejs2

I am developing an SPA using Vue 2.0. The components developed so far are for the "desktop" browsers, for example, I have

Main.vue, ProductList.vue, ProductDetail.vue,

I want another set of components for the mobile browsers, such as MainMobile.vue, ProductListMobile.vue, ProductDetailMobile.vue,

My question is, where and how do I make my SPA render the mobile version of components when viewing in a mobile browser?

Please note that I explicitly want to avoid making my components responsive. I want to keep two separate versions of them.

Thanks,

like image 679
sean717 Avatar asked Jan 30 '18 06:01

sean717


People also ask

How do I switch between Vue components?

You need to bind the value into the component and then emit a new value to the parent when you click a link. If you emit the event input and bind the value value , then you can use v-model .

How do I share data between Vue components?

There are only three different ways to share data across the VueJs components, it depends on you which technique is better for you to solve your problem. Using props share data from parent to child. Using Event Emitting custom events to share data from child to parent.

Is mobile VueJS?

Vue Native is a JavaScript framework designed to help you build cross-platform mobile applications with JavaScript. Vue Native is a wrapper around React Native that enables you to build mobile applications using Vue. js.

How do I view Vue files in my browser?

Open in browser To view the project, open a tab and type http://localhost:3000 into the URL bar. That's the address that the Node server is listening to. You should now see this page, which is the basic structure of our project.


2 Answers

I have simple solution for Vue.js:

<div v-if="!isMobile()">   <desktop>   </desktop> </div> <div v-else>   <mobile>   </mobile> </div> 

And methods:

methods: {  isMobile() {    if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {      return true    } else {      return false    }  } } 
like image 199
Zhanserik Avatar answered Oct 01 '22 04:10

Zhanserik


I have an idea, use a [mixin][1] which detects if the browser is opened on mobile or desktop (example for js code in this [answer][2] ).. then use v-if, for example

<production-list v-if="!isMobile()"></production-list> <production-list-mobile v-else></production-list-mobile> 

so here is an example on https://jsfiddle.net/Ldku0xec/ [1]: https://vuejs.org/v2/guide/mixins.html [2]: Detecting a mobile browser

like image 30
Mohd_PH Avatar answered Oct 01 '22 04:10

Mohd_PH