Is it considered proper to use an axios method within the created hook?

Currently, I am working on a VueJS project and would appreciate feedback on whether my code aligns with good coding practices:

created(){
    this.getData()
}
methods: {
    getData(){
       // axios request
    }
}

Answer №1

It is acceptable to invoke a Vue function from the created life cycle hook. This action is completely valid 👌

Answer №2

Check out these demonstrations that align with your current project:

You can also reference the VueJs documentation for more examples.

Indeed, they are tackling similar tasks.

Answer №3

When adding additional content to your block that relies on fetched data, it is important to remember to use the await keyword before accessing the data.

async created(){
    await this.fetch()
    // additional logic that depends on the fetched data goes here
}
methods: {
    fetch(){
       // axios request
    }
}

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

Transforming a data array into JSON format with the help of a property mapping technique

I am attempting to transform a 2d array into a JSON object using a key map. The key map is defined as var keys = ['id', 'title', 'customer.id', 'customer.name', 'customer.phone.home', 'customer.phone.m ...

Attempting to upload an item using the OBJLoader tool on my own computer | Three.js

I've been experimenting with integrating 3D objects into a web page using three.js and a .obj file. Despite my efforts to use OBJLoader to load the file onto the HTML view, I've encountered some difficulties. I even tried different loaders, but n ...

Refreshing AvForm in React.js to clear entered data

Currently, I am utilizing AvForm within React JS to handle form submissions. However, I am encountering an issue where the form does not reset after submission; instead, it retains the previous input values until the page is manually refreshed. Here is a ...

Leveraging $last in Angular for obtaining the final visible element with ng-show

My ng-repeat container div contains multiple images, each with a corresponding div next to it. Using $last, I can make sure only the div of the final image is visible after all images. However, I also have a filter button on the page that hides certain im ...

Guide to incorporating three-js-csg into Angular 6

Initially, the following steps are taken: npm install three --save npm install @types/three npm install three-js-csg --save Following that: import * as THREE from 'three'; import * as csg from 'three-js-csg'; let sphere1BSP = new ...

What is the best way to transmit two distinct sets of data from a child component to the v-model of a parent component?

Currently, I am working on a project using vuejs 2 and typescript. In this project, I need to pass two different sets of data - data and attachments - within the parent component. I am utilizing vue-property-decorator for this purpose. However, I am facing ...

Can you explain the meaning of the second line in the code snippet?

Could you please explain the meaning of the second line in this code snippet? Is it a ternary operation, or something else entirely? And what is its significance? const user = await User.findOne({ email: req.body.email }); !user && res.stat ...

Display the array of selected values on the console upon clicking the submit button

After creating a function that saves checked values in the 'Arr' array, I am facing an issue where the array is being printed multiple times when the "View Results" button is clicked. This results in the checked values appearing multiple times on ...

Developing a way to make vue-custom-element compatible for embedding in external websites

I've been exploring ways to use a component from my Vue website on another site by embedding it in HTML. I came across https://github.com/karol-f/vue-custom-element and found this helpful guide. After installing the npm packages vue-custom-element an ...

Is there a way to show information exclusively on the selected card upon clicking?

[click here to see image 1] https://i.sstatic.net/cfvUT.png [click here to see image 2] https://i.sstatic.net/ISXAU.png Greetings, fellow beginners! Once again I find myself stuck on a coding problem. I have fetched various workout data and displayed t ...

Deactivating the drag feature when setting the duration of a new event in FullCalendar

Hello there! I've integrated full calendar into my Angular project and I'm facing a challenge. I want to restrict users from defining the duration of an event by holding click on an empty schedule in the weekly calendar, where each date interval ...

The combination of Perlin Noise, Erlang, and JavaScript calls for a

Is it possible to generate the same noise map using a perlin/simplex noise generation algorithm with the same seed in two different programming languages? I am looking to create a procedurally generated multiplayer world where javascript clients and an er ...

Sending an object as a prop in React component

I have a function class: function TopicBox({topicName, article1}) { return ( <div className="TopicBox"> <h1>{topicName}</h1> <div className="topicDivider" /> <Ar ...

Modify a section of an HTML text's formatting

function changeEveryCharColor(id) { var part; var whole = ""; var cool1 = document.getElementById(id).innerHTML; console.log(cool1); for(var i = 0; i < cool1.length; i++) { color1 = getRandomInt(255); ...

ng-if behaving unexpectedly

This is a demonstration of how I populate the Table and attach checkboxes to a controller: <tr ng-repeat="key in queryResults.userPropNames"> <td><input type="checkbox" data-ng-checked="selectedKeys.indexOf(key) != -1 ...

What causes $(this) to stop functioning post successful execution?

Here are the code snippets I'm currently working with: $(this).removeClass('page larger').addClass('current'); $(this).siblings().removeClass('current').addClass('page larger'); Interestingly, when I try to pl ...

What is the best way to ensure that a navbar dropdown appears above all other elements on

I'm having trouble creating a navbar dropdown with material design. The dropdown is working fine, but the issue I'm facing is that other elements are floating above it. https://i.stack.imgur.com/aJ0BH.png What I want is for the dropdown to floa ...

Trouble with V-if not updating after property is modified within an async TypeScript function

There is a scenario where one HTML element becomes hidden after an async call returns from the server, while another HTML element is displayed: <template> <div v-if="!showElementTwo">Element 1</div> <div v-if="show ...

How can I apply a CSS class to a group of radio buttons within a radio button list?

I'm attempting to validate a radio button group and apply a specific CSS class to it. Since this is a server control, I can only apply the class one way. However, upon checking with jQuery, I discovered that the class is actually being attached to the ...

What is the best way to extract data from a JavaScript object received from multer?

Currently, I am facing an issue while trying to utilize multer for handling the upload of a CSV file in Express. The goal is to parse the uploaded file line by line. Although I can successfully retrieve the file as an object stored in req.body, I encounter ...