How to implement responsive layout in vue-cli

How to implement responsive layout in vue-cli

When we are doing front-end development, we will inevitably encounter the adaptation of PC and mobile terminals. Faced with such a problem, some companies will prepare two pages, mobile is mobile, PC is PC, and the responsive layout is to display different pages according to the different models of users. Without further ado, just paste the code

First of all, this function is implemented with the help of vuex. If you haven't downloaded it yet, you can download it first.

yarn add vuex

Since it is a responsive layout, we need to prepare two sets of CSS, one is the CSS for the PC side, and the other is the CSS for the mobile side. For the time being, we will call the PC side style computer and the mobile side style mobile.

The first thing we need to do is the current screen width

state: {
  screenWidth: document.documentElement.clientWidth,
  screenHeight: document.documentElement.clientHeight
 }

This is the state of vuex. We will monitor the screen width in real time later, so we also need to provide a method to change screenWidth, so I wrote another mutation

mutations:
  setWidth(state, value) {
   state.screenWidth = value
  },
  setHeight (state, value) {
   state.screenHeight = value
  }
 }

In this way, our vuex file is written, and then it is APP.vue. We need to add a window.onresize event under this file to update the screenWidth value in vuex in real time. Here we use the auxiliary function

import { mapState, mapMutations } from 'vuex'

Add events to the mounted hook function

mounted () {
  window.onresize = () => { 
   this.setWidth(document.documentElement.clientWidth) 
  }
}

For example, for the navigation in our web page, we need it at the bottom on mobile and at the top on PC, so we can use watch or computed to monitor the value of screenWidth in the nav.vue component. Here we use computed

<ul :class="computedPhone">//Provide different class names according to screenWidth<router-link to='/file' tag="li" active-class="active">Movie</router-link>
   <router-link to='/cinma' tag="li" active-class="active">cinema</router-link>
   <router-link to='/center' tag="li" active-class="active">Personal Center</router-link>
</ul>

computed: {
  ...mapState('stylesheet', ['screenWidth']),
  computedPhone() {
   if (this.screenWidth < 1024) {
    return 'mobile'
   } else {
    return 'computer'
   }
  }
 }

In this way, you can achieve responsive layout by writing two sets of css

This is the end of this article about how to implement responsive layout in vue-cli. For more relevant vue-cli responsive layout content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue responsively adds and modifies array and object values
  • A brief discussion on the responsiveness principle of Vue
  • Use Vue.set() method to implement responsive modification of array data steps
  • Detailed example of Vue's event-responsive progress bar component
  • How to implement a responsive system in Vue
  • Vue3.0 responsive system source code line by line analysis
  • Detailed explanation of Vue's responsiveness principle
  • Use JS to responsively modify the input value of the Vue implementation page
  • A brief discussion on the principle of Vue data responsiveness

<<:  How to write DROP TABLE in different databases

>>:  Solution to MySql service disappearance for unknown reasons

Recommend

Summary of basic knowledge points of MySql database

Table of contents Basic database operations 2) Vi...

Vue component to realize carousel animation

This article example shares the specific code of ...

Detailed explanation of MLSQL compile-time permission control example

Preface The simple understanding of MySQL permiss...

MySQL 8.0.16 installation and configuration tutorial under Windows 10

This article shares with you the graphic tutorial...

Summary and practice of javascript prototype chain diagram

Table of contents Prototype chain We can implemen...

Detailed explanation of the use of MySQL Online DDL

Table of contents text LOCK parameter ALGORITHM p...

How to delete extra kernels in Ubuntu

Step 1: View the current kernel rew $ uname -a Li...

What to do if the container started by docker run hangs and loses data

Scenario Description In a certain system, the fun...

Encapsulate the navigation bar component with Vue

Preface: Fully encapsulating a functional module ...

Mysql string interception and obtaining data in the specified string

Preface: I encountered a requirement to extract s...

CSS border adds four corners implementation code

1.html <div class="loginbody"> &l...

Four modes of Oracle opening and closing

>1 Start the database In the cmd command windo...

Does MySql need to commit?

Whether MySQL needs to commit when performing ope...

Methods for optimizing Oracle database with large memory pages in Linux

Preface PC Server has developed to this day and h...

How to use the jquery editor plugin tinyMCE

Modify the simplified file size and download the ...