Create a Vue module named MyModule and assign it to the Vue instance

Adding functionality to Vue:

import api from '@/js/api'
Vue.prototype.$api = api

In my custom api, I can refer to the Vue instance using this with a default exported function.

//api.js
import Vue from 'vue'

export default function () {
   console.log('default export', this)
   //=> Vue
}

To call this function, use Vue.prototype.$api().

Now, I want to enhance my api.js by including multiple functions and private variables while retaining the reference to the Vue instance via this, similar to when using the axios library.

I aim to export a module without losing the context of this.

var MyModule = {
    this.fn1 = function(){console.log(this) //=> Vue}
    this.fn2 = function(){console.log(this) //=> Vue}
}

How can I achieve this?

Answer №1

Unfortunately, this cannot be achieved.

The this reference within the .then() function is determined by the arrow function it is nested within.

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

After successfully upgrading Vue2 to Vue3, I encountered an error related to Vutify

Currently working on upgrading my Vue2 to Vue3 project while integrating Electron for a Desktop application. Vue Version: "^3.3.4", Vuetify: "^3.4.0-beta.1", Ran into an issue during compilation. ./node_modules/vuetify/lib/framework.m ...

Vue Component (Nuxt.js) encountering issue with processing Bootstrap image asset in props style attribute

Attempting to import an image asset using a relative path for a banner component has presented some challenges. The standard method seems to work without any issues. <b-img src="~/static/images/carousel1.jpg" alt="Samyojya Consultants ban ...

What is causing Vue to not update a list when using props?

When my App loads, I make an API call in the mounted() method to retrieve a JSON with a list of items. After that, I update the prop set in my target component Homepage: Homepage.pages = resJSON.data.pages; Take a look at the App code below: <template& ...

Why am I receiving a successful preflight OPTIONS response, yet still encountering a CORS error when making a POST request?

I am facing an issue with CORS while making POST requests from my Vue front end to a Node back end. Even though I am using the cors library in the backend and preflight request is successful, the actual POST request is still failing with a CORS error. Aft ...

Display or conceal elements in a v-for select input by leveraging the value of another input with Vue3

In my form, I have two dropdown select option fields generated using loops from predefined constants. One dropdown is for selecting different "sides" and the other for choosing various "caps". When a user selects "2-Sided" in the sides dropdown, I want t ...

Vue Progressive Web App pre-caching routes for improved performance

Seeking guidance to ensure I'm on the right track. I've developed a simple web application using Vue CLI with PWA support. Everything appears to be functioning properly, including the install prompt. My objective is to cache certain pages (route ...

What are the steps to ensure a successful deeplink integration on iOS with Ionic?

Recently, I was working on a hybrid mobile app for Android/iOS using Nuxt 3, TypeScript, and Ionic. The main purpose of the app is to serve as an online store. One important feature involves redirecting users to the epay Halyk website during the payment pr ...

As the user types, the DD/MM/YYYY format will automatically be recognized in the

In my React component, I have an input box meant for entering a date of birth. The requirement is to automatically insert a / after each relevant section, like 30/03/2017 I am looking for something similar to this concept, but for date of birth instead of ...

Learn how to integrate Bootstrap with Vue.js TreeView in this tutorial

If you're looking to create a treeview using Vue.js, the code structure would resemble something like this: HTML: <!-- item template --> <script type="text/x-template" id="item-template"> <li> <div ...

Trouble accessing value in Vue.js

I'm having an issue where I am unable to access this.active in my method delete. What could be causing this problem? data () { return { ride: { user: {}, location: {}, type: {} }, active: false } }, methods: { delete () ...

The attribute 'positive_rule' is not found within the structure '{ addMore(): void; remove(index: any): void;'

data() { return { positive_rule: [ { positive_rule: "", }, ], }; }, methods: { addMore() { this.positive_rule.push({ positive_rule: "", }); }, ...

send the value of a variable from a child component to its parent

I have created a typeahead component within a form component and I am trying to pass the value of the v-model from the child component to its parent. Specifically, I want to take the query model from the typeahead component and place it in the company mode ...

What is the best way to dynamically apply the "active" class to a link when it is clicked

Here is how my vue component looks: <template> <div> ... <div class="list-group"> <a :href="baseUrl+'/message/inbox'" class="list-group-item"> Message </a> ...

choose a selection hopscotch

Is there a way to prevent the input field from jumping out of alignment when an option is selected in these q-select inputs? I want to fix this issue so that the field remains in line with the horizontal alignment. https://codepen.io/kiggs1881/pen/RwENYEX ...

Error message in vuejs: JSON parsing error detected due to an unexpected "<" symbol at the beginning

I have been trying to troubleshoot this issue, but I am having trouble understanding how to resolve it. Currently, I am using lottie-web in a project and need to set the animation parameters on an object in order to pass them as a parameter later. This i ...

After a complete page refresh, Vue.js route parameters are not retained

When passing a user id from the router-link using params to display the user's detail on the profile page, everything works correctly. However, upon reloading the page, $route.params.id changes to undefined and the displayed data disappears. To retrie ...

Whenever I navigate to this specific route, I consistently encounter an error message in my console

VM28353:1 Error: Unexpected token 'o' found in JSON at position 1 at JSON.parse (<anonymous>) at getUser (auth.js?c7d4:11) at wrappedGetter (vuex.esm-browser.js?5502:335) at Object.eval [as getUser] (vuex.esm-browser.js?5502 ...

What steps should I take to ensure a Vue 2/Vuetify form does not submit if the mandatory fields are left blank?

I developed a contact form using Vue 2/Vuetify 2 for a project I am working on. The form successfully submits to Strapi, the CMS that I am utilizing. However, despite having set rules and validation, it still submits with empty fields. The best progress I ...

Integrate Tailwind CSS into the bundled JavaScript file

Is there a way to integrate tailwind css into bundle js effectively? Take a look at this example involving vue 3 and tailwind 3 https://github.com/musashiM82/vue-webpack. Upon executing npm run build , it generates 3 files: app.js ABOUTPAGE.js app.6cba1 ...

Use Vue JS to send a GET request to the server with custom header settings

When creating a Vue.js component, I want to send a GET request to a server with specific headers. How can I achieve this? Using cURL: -X GET "http://134.140.154.121:7075/rest/api/Time" -H "accept: application/json" -H "Authorization: Bearer eyJhbGciOiJSUz ...