Expanding worldwide mixin in Nuxt.js

Currently, I am working with Nuxtjs version 2.15.7 and have a mixin file structured like this:

utils.js :

import Vue from "vue"

if (!Vue.__utils__) {
  Vue.__utils__ = true
  Vue.mixin({ 
    data(){
      return{
         ...
      }
    },
    methods:{
      ...
    }
  })
}

Now, I am looking to create another global mixin that builds upon my existing utils.js file. Here is an example of what I want to achieve:

utils-another.js :

import utils from '@/plugins/mixins/utils.js'
import Vue from "vue"

if (!Vue.__utils__) {
  Vue.__utils__ = true
  Vue.mixin({ 
    // ... codes from utils.js
    data(){
      return{
         ...
      }
    },
    methods:{
      ...
    }
  })
}

I need to be able to choose between using utils.js or utils-another.js based on a specific environment property. How should I go about implementing this without duplicating all the code from utils.js into utils-another.js for easier maintenance?

Answer №1

You can organize your mixin code like this

// customMixin1.js
export default {
  data() {
    return {
      //...
    }
  },
  methods: {
    //...
  }
}

// customMixin2.js
export default {
  data() {
    return {
      //...
    }
  },
}

This allows you to create two global mixins.

// utils-custom.js:
import customMixin1 from '@/plugins/mixins/customMixin1.js'
import customMixin2 from '@/plugins/mixins/customMixin2.js'
import Vue from "vue"

if (!Vue.__custom__) {
  Vue.__custom__ = true
  Vue.mixin({
    mixins: [customMixin1, customMixin2], // custom.js codes
  })
}

Alternatively, you can use just one mixin

// utils-custom.js:
import customMixin1 from '@/plugins/mixins/customMixin1.js'
import Vue from "vue"

if (!Vue.__custom__) {
  Vue.__custom__ = true
  Vue.mixin({
    mixins: [customMixin1], // custom.js codes
  })
}

Exercise caution when using global mixins, as they impact every Vue instance created, including third party components.

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

Issue arises when child component fails to receive updated props from parent component after data changes in parent component

I am working with a template named Report.vue, which contains a data() property called dataSent:[]. Within this template, I have a component called BarChart that should receive an updated array from the dataSent property to display graphs. However, when I ...

Issues with data within a Vue.js pagination component for Bootstrap tables

Currently, my table is failing to display the data retrieved from a rest api (itunes), and it also lacks pagination support. When checking the console, I'm encountering the following error: <table result="[object Object],[object Object],[object Ob ...

Struggling to integrate a JavaScript sdk with an Angular2 application due to missing dependencies

I've been struggling to incorporate the Magic: The Gathering SDK library into my Angular2 application. I've tried various methods, but nothing seems to work seamlessly. When I attempt to import the library using TypeScript like this: import { } ...

Creating a captivating animation for a social media like button with CSS

I came across some animation code on the web and noticed that when I click the image, it replays the animation from the starting point. I want to make it so that when I click the image, the animation plays and stops, and if clicked again, resets the image. ...

Using NodeJS and Express together with Ajax techniques

I am currently developing a web application that utilizes Ajax to submit a file along with some form fields. One unique aspect of my form is that it allows for dynamic input, meaning users can add multiple rows with the same value. Additionally, the form i ...

Update the ajax request from jquery to axios and configure the xhrFields

Can someone help me convert my jQuery request to axios? $.ajax({ type: "GET", url: "http://6232423.212342343.100.89:9000/api/v2/content/categories/", xhrFields: { withCredentials: true }, }); I attempted to do it like this: axios ...

Initiating, halting, and rejuvenating a timer in JavaScript

Here is a simple code snippet where I'm experimenting with starting, stopping, and resetting a JavaScript timer. The goal is to have a timer running on a page that sends a message once it reaches the end of its countdown before restarting. The stop b ...

Steps for making a cookie in Node.js using Socket.IO

I'm currently exploring the process of creating a cookie while utilizing socket.io. Although I have successfully figured out how to read cookies, I am struggling to find resources on how to actually create one. socket.on('test', async funct ...

The dynamic styles set by CSS/JavaScript are not being successfully implemented once the Ajax data is retrieved

I am currently coding in Zend Framework and facing an issue with the code provided below. Any suggestions for a solution would be appreciated. The following is my controller function that is being triggered via AJAX: public function fnshowinfoAction() { ...

The functionality of Bootstrap's Modal feature seems to be malfunctioning

I'm attempting to test the sample modals for my project, but even the codes from jfiddles are not working for me. When I click the button, it only gives me a dimmed window. I have also tried searching on Google for a solution, but to no avail. Below ...

How can I pass an argument to Node within a package.json script instead of the script itself?

In my project's package.json, I have a script that looks like this: "scripts": { "test": "... && mocha --full-trace test/db test/http test/storage test/utils", I am trying to pass the --async-stack-traces o ...

Using Vue.js to conditionally render content based on changes in a variable

I am facing a challenge in rendering a new element once the boolean variable waiting changes to true. The issue arises when transitioning from one v-if statement to another, as the boolean does not update until the first statement is completed. How can I s ...

What is the process for inserting text or letters into a checkbox using Material Ui?

I am looking to create circular check boxes with text inside them similar to the image provided. Any help or guidance on achieving this would be greatly appreciated. View the image here ...

"Effortlessly Arrange Multiple Gridview Rows Using Checkbox Selection in jQuery Drag and Drop

Struggling with getting checkboxes clicked when a GridViewRow becomes selected. Utilizing jQuery Methods like .sortable and .selectable. Worked as intended but looking to have a checkmark placed on the checkbox cbBulkSort within the GridView's first c ...

Is it necessary to compile Jade templates only once?

I'm new to exploring jade in conjunction with express.js and I'm on a quest to fully understand jade. Here's my query: Express mentions caching jade in production - but how exactly does this process unfold? Given that the output is continge ...

Having trouble getting the timer function to execute upon page load in JavaScript

My goal is to have my basic timer function activate when the page loads. However, I'm encountering issues with it not working as intended. I suspect there may be an error in the if else loop, possibly here: setTimeout(function(tag, sec), 1000);. How c ...

It requires two clicks on the button for the Vue and Pinia store to update

I've been experimenting with Vue and trying to understand it better. When I click the button in LoginForm.vue for the first time, both token and user_data are null. It's only on the second click that they finally update. How can I ensure these va ...

White border appears when hovering over MUI TextField

I've been troubleshooting this issue for what seems like an eternity. I've combed through Chrome's inspect tool, searching for any hover styles on the fieldset element, but to no avail. Here's my dilemma... I simply want a basic outline ...

Filtering an Array in VueJS Based on User Input

Currently, I am working on a Vue.js application where my goal is to filter an array based on user input from a form. The issue I am facing is that the array named autocomplete is not being populated with visitors that match the query of the first name. T ...

Looking to update a specific element in an array on an hourly basis?

var temperature = [-18 , -18.1 , -18.2, -18.3, -18.4, -18.5, -18.6, -18.7,-18.8, -18.9, -19 , -19.1 , -19.2, -19.3, -19.4, -19.5, -19.6, -19.7,-19.8, -19.9, -20]; $(".warlotemp").html(temperature[0]); $(".warlotemp").append(" C"); I am intere ...