Error: The axios instance for the app is not defined in Vue.js 3

My main.js file is responsible for creating an app and adding components like router, store, and axios.

Here is how I'm globally adding axios:

import {createSSRApp, createApp, h} from 'vue';
import axios from 'axios'    

 const rootComponent = {
        render: () => h(App),
        components: { App }
    }

const app = createApp(rootComponent);
app.config.globalProperties.$axios=axios;

if (process.env.NODE_ENV === 'development') {
        app.$axios.defaults.baseURL = 'http://localhost:8080'; // This triggers an error
}

However, when I try to run this in the browser using webpack-dev-server, I encounter the following error:

Uncaught TypeError: app.$axios is undefined

I'm confused as to why app.$axios is showing as undefined. Can anyone help explain this?

Answer â„–1

Because the global property is accessible on the instance, not the prototype.

documentation

globalProperties

Introduces a global property that can be utilized in any component instance within the application. The component’s property will take precedence in case of conflicting keys.

Thus, it can be used within the app or component instances through this

this.$axios.defaults.baseURL = 'http://localhost:8080';

Alternatively, if you are using it outside the app instance, you can access it in the same manner as originally defined

// within the same file, as the axios object is already available
axios.defaults.baseURL = 'http://localhost:8080'; 

// or from another location
app.config.globalProperties.$axios.defaults.baseURL = 'http://localhost:8080';

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

Incorporate a fresh label for a function utilizing AngularJS

I want to insert a new HTML tag with an event attached to it. Here is an example of what I am trying to achieve: <html ng-app="module"> <head> <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script&g ...

The functions that have been imported are not defined

I encountered a Error in created hook: "TypeError: _api__WEBPACK_IMPORTED_MODULE_0__.default.$_playerApi_getPlayers is not a function" issue while using Webpack through Vue CLI on my webpage. Below is the structure of my project directory: . + main.js + ...

Create a duplicate of the information stored within the $scope variable

Suppose there are multiple variables stored in a $scope and the objective is to create a new object that only includes those variables, excluding all the internal Angular-related data. The specific names of the $scope variables may not be known. This can ...

Hold off on submitting the form until the location has been obtained

I am facing an issue with my application where it tries to retrieve location settings from the browser, which takes some time. I would like this process to run when the page loads so that the data is available when needed. However, if a user clicks the s ...

How to style the first dropdown value in AngularJS to appear bold?

Is there a way to style only the first value in a dropdown list as bold without using jQuery? Here is the code for the dropdown: <div class="col-xs-3"> <select-box id="ad-version-select" options="curItem.stats.version" model="state.version" i ...

Guide to making an object with a value sourced from the redux store

I am looking to develop an object with custom getters and a key representing language. Depending on the language key, different values should be returned by the getters. This is specifically for a customizable language selection feature. As an example, one ...

Manipulate CSS Properties with Javascript Based on Dropdown Selection

I am currently working on implementing a feature that involves changing the CSS property visibility: of an <input> element using a JavaScript function triggered by user selection in a <select> dropdown. Here's what I have so far in my cod ...

How can I maintain focus selection while replacing HTML with text in a contenteditable div without losing it?

When working with a div tag that needs to be editable, the goal is to prevent users from entering any HTML into the tag. However, despite efforts to restrict input, when users copy and paste content, it often includes unwanted tags. To address this issue, ...

What is the method for extracting search parameters as an object from a URL that includes a hash symbol?

Currently, I am dealing with a URL structured in the following format: https://my-app.com/my-route/someOtherRoute#register?param1="122"&param2="333" While I am familiar with fetching query strings from a standard URL, I am struggli ...

Steps for converting an HTML form into a sophisticated JavaScript object

Is it possible to transform a form into a complex JavaScript object based on a structured form layout? I am not sure if there is a better way to accomplish this, but essentially what I am looking for is the following scenario: <form> <input n ...

Using the drag and drop feature with the v-textarea component in Vuetify

Struggling to incorporate a Drag and Drop feature on vuetify v-textarea. Encountering an issue where clicking on the v-textarea results in the value from a dropped component being removed. Unsure if it's a flaw in my code or a limitation in vuetify v- ...

Troubleshooting custom filtering with jQuery Datatables across different tables is presenting challenges

I am currently utilizing the Datatables 1.10 jQuery plug-in and I am interested in implementing the custom search feature to filter two tables simultaneously on a single page, as shown below: function applyFilterByErrorClass(propertiesTable, errorClassNam ...

Utilizing Node.js with Redis for organizing data efficiently

Currently, I am in the process of configuring a Redis cache system for storing incoming JSON data in a specific format. My goal is to create an ordered list structure to accommodate the large volume of data that will be stored before eventual deletion. Th ...

Swapping out data within a container

I've been experimenting with creating a hangman game using JavaScript and HTML. However, I'm facing an issue where clicking on a letter doesn't replace the "_" placeholder. var myList=["Computer","Algorithm","Software","Programming","Develop ...

Dealing with problems in col-md display on tablet devices

When viewing on Full Screen and mobile, the ranges panel (class="col-md-3") displays correctly. However, on tablet screens, the left column is obscured by the youtube image panel (class="col-12 col-md-9"). I have attempted various adjustments to the div s ...

Using Javascript within HTML: A guide on when to utilize semi-colons and when to implement the return false statement

Exploring the use of JavaScript attributes within HTML elements: <input type="button" onclick="somceFunc()" /> <input type="button" onclick="somceFunc();" /> <input type="button" onclick="somceFunc(); return false;" /> Debating whether ...

How can Node.js improve callback functions and integrate nodemailer for optimal performance?

I'm currently working on a new feature that involves sending a post request to download HTML pages from specific URLs, zip them, and then email the zipped file to a designated email address. The endpoint for this route looks like http://localhost:3000 ...

Issue with displaying Buefy Material Design icons when using the b-icon component

This is my first time using Buefy I encountered an issue with the b-icon component, Rather than displaying the icon, I saw an empty block element which was confusing. Switching to a different icon pack did not resolve the problem either. ...

Is it possible to implement jQuery events on all elements belonging to a specific class

Hey there, I'm facing a little challenge with my code. Currently, I have a snippet that allows a user using iOS to drag around a <div> with the class .drag on the webpage. Everything works perfectly when there's only one instance of .drag, ...

What is the extent to which a scope variable will be accessible within the link function of Angular?

var $directive = angular.module('myApp', []); $directive.directive('myDirective', function(){ return { restrict: 'E', template: '<h4>{{title}}</h4>' compile: function(element, attrs){ ...