Building Your Own Custom Mobile Global Breakpoint Plugin with Vuetify in Nuxt.js

Looking to set up a custom breakpoint system for my Nuxt/Vuetify project so I can easily manage it from one centralized location instead of using $vuetif.breakpoint..... etc

To achieve this, I have created a plugin file named mobile.js. While it functions properly, I have noticed that it is slower than the standard breakpoints.

For example,

<span v-if="mobile"><show</span>
<span v-if="$vuetify.breakpoint.smAndDown"><show</span>

Both lines work as expected, but the first line (the mobile plugin) has a delay. I am looking to optimize my plugin for faster performance.

This is my current plugin (mobile.js)

import Vue from 'vue'

const mobilex = {
  data() {
    return {
      mobile: false,
    }
  },

  beforeDestroy() {
    if (typeof window !== 'undefined') {
      window.removeEventListener('resize', this.onResize, { passive: true })
    }
  },

  mounted() {
    this.onResize()
    window.addEventListener('resize', this.onResize, { passive: true })
  },
  methods: {
    onResize() {
      this.mobile = window.innerWidth < 960
    },
  },
}

Vue.mixin(mobilex)

In nuxt.config.js

  plugins: [  '~/plugins/mobile' ],

Answer №1

For your code to function properly, it must be run on the client-side since the window object is not defined on the server-side. This explains why there may be a delay in execution.

If you require faster performance, consider utilizing a mobile detection library like @nuxtjs/device or inspecting the User Agent.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing the <td> onclick event to fail in asp.net?

Having an issue with making a <td> clickable to display a div. Check out my code snippet below: <td id="tdmord" style="padding-left: 15px; color: #86A7C5; padding-right: 15px; font-family: Arial; font-size: small;" onclick="return showDiv1()"& ...

Issue with Highcharts: The useHTML flag is not functioning properly when trying to render labels

Currently, I am utilizing highcharts and a phantomjs server for rendering charts and labels. However, I have encountered an issue where the useHTML flag does not function as expected when rendering the labels. Following the instructions in the documentatio ...

Frequently, cypress encounters difficulty accessing the /auth page and struggles to locate the necessary id or class

When trying to navigate to the /auth path and log in with Cypress, I am using the following code: Cypress.Commands.add('login', (email, password) => { cy.get('.auth').find('.login').should(($login) => { expect($log ...

Uncertain about the process of accessing req.body data

I'm having trouble retrieving the body data on my server, even with simple .get requests. Despite simplifying it to just a basic call, I still can't seem to access the body data. This issue is hindering more complex calls that require accessing t ...

Disable the outer div scrolling in VueJS, but re-enable it once the inner div has reached the bottom of

I am currently working on a webpage that contains multiple divs stacked vertically. Here is the concept I am working with: Once the scrollbar reaches the bottom of the first div, the outer scrollbar will be disabled and the inner scrollbar will be enabled ...

Checkbox Selection - Modify Prompt to "Kindly mark this box to continue"

I have created a multi-select checkbox form, and I've added some JavaScript to ensure that the visitor selects at least one option <div class="form-group options"> <label class="control-label col-md-4" for="optiontext">Specify an option&l ...

Leveraging Arrays with AJAX Promises

I am currently working on making multiple AJAX calls using promises. I want to combine the two responses, analyze them collectively, and then generate a final response. Here is my current approach: var responseData = []; for (var i=0; i<letsSayTwo; i++ ...

Changes to Vuex state do not automatically trigger a refresh of the Vue components in a web environment

Having just started working with Vue, I am facing some challenges with the existing code that involves computed properties of my Vuex objects. computed: { ...mapGetters([ 'selectedObject' ]) }, When I add a new object to my array in Store.js, ...

Tips for directing your attention to a specific cell within a grid after inputting a value into a textBox

I am looking to navigate to a specific cell within a grid by entering the cell number into a textBox. Upon typing any number in the textBox, it should automatically focus and scroll to that particular cell in the grid. For example, if I type 601 in the te ...

What are the different ways to interact with the object retrieved from the onloadedmetadata event

Having trouble accessing a specific value in an object that is the result of the onloadedmetadata event. When I log the entire object using audioDuration, I am able to see and retrieve the respective value without any issues. However, when I try to access ...

Vuex getters not displaying expected values in computed properties until entire page has finished loading

When working with computed properties using data fetched by the mapGetters function in my Vuex store, I always encounter the issue of getting an undefined value until the entire page or DOM is fully loaded. For instance, I have an example of an isRegister ...

Bidirectional Data Flow with rxjs in Angular

After adapting an Angular template and component from Angular Material's dashboard schematic, I wanted to manipulate the properties on the "cards" object using events and two-way data binding. Initially, it seemed like two-way data binding was working ...

Unlocking the Secrets: Javascript Tricks for Retrieving Object Values

I have an item that contains data which I need to display. {"text": "active user active user213123 idle user234234234 loggedout userafdadf" }, To extract the content, I used the following code: Response = message.s ...

Arrays contain multiple elements, not just a single item

Is it possible to display multiple objects in one container? For instance, I have an array let array = [1, 2, 3, 4, 5, 6, 7, 8, 9]; array.forEach((item, index) => ( <div> <div> item 1, 2, 3 </div> <div> item 4, 5, 6 </div ...

Values are established in the $(document).ready() function using jQuery

Is it possible to set values for objects after the entire DOM has finished loading? I seem to be encountering a null pointer exception when triggering onBlur and onFocus events from a textbox. Can anyone spot what I might be doing incorrectly? Javascript: ...

Finding the identifier for resources through excluding external influences

I am currently facing an issue with the full calendar plugin. In my set up, I have 3 resources along with some external events. The problem arises when I try to drop an external event onto the calendar - I want to retrieve the resource id from which the ev ...

Occasionally, the NodeJs server will freeze up and require a forced stop via ctrl+c to

My node.js server is set up to handle ajax requests. When I click on the webpage, an ajax call is sent and the function runs smoothly most of the time. However, there are instances where the server freezes. In some cases, using ctrl+c unfreezes it and it c ...

When using Javascript, the click function is returned as not being a valid function

I am working on a project with two PHP files - index.php and loadimages.php. The index.php page contains a thumbnail gallery and a canvas. The images in the thumbnail gallery are populated by loadimages.php. Here is a snippet of the code from loadimages.ph ...

Updating a slider based on the values of two other sliders can be achieved by implementing a

I am working on a project that involves three input sliders. I need the third slider to display the product of the values from the first two sliders. Additionally, I want the value of the third slider to update dynamically whenever the values of the first ...

Display live data directly from a specific XML tag on a webpage

My project involves working with a local XML file that is being constantly updated by a third party. I am looking to create a web page that can dynamically read and display a specific XML tag without requiring the page to be refreshed. Additionally, I woul ...