Home >Web Front-end >Vue.js >Vue and Axios tutorials that even beginners can learn to help you get started with front-end development
Vue and Axios tutorials that even beginners can learn, taking you to play with front-end development
1. Introduction to Vue
Vue is a front-end development framework that can help us build interactive user interface. Compared with other frameworks, Vue is more lightweight, easy to learn and use, and is suitable for project development of all sizes.
1.1 Vue installation
First, we need to install Vue. You can install it in the following ways:
1.1.1 Use the package management tool to install
Use npm or yarn to install Vue in the command line:
npm install vue
or
yarn add vue
1.1.2 Use CDN to import
Add the following code in the HTML file:
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
1.2 Hello World
Add a container in the HTML file:
<div id="app"> {{ message }} </div>
Add the following code in the JavaScript file:
var app = new Vue({ el: '#app', data: { message: 'Hello World!' } })
Open the browser, you will see the words "Hello World!"
2. Introduction to Axios
Axios is a Promise-based HTTP client, used to send HTTP requests in browsers and Node.js. It can request data across domains on different platforms, and supports request interception, response interception and other functions.
2.1 Installation of Axios
Use the following command to install Axios:
npm install axios
or
yarn add axios
2.2 Send a GET request
in the JavaScript file Add the following code:
axios.get('/api/users') .then(function (response) { console.log(response.data); }) .catch(function (error) { console.log(error); });
The above code sends a GET request and prints out the returned data.
2.3 Send a POST request
Add the following code in the JavaScript file:
axios.post('/api/users', { firstName: 'John', lastName: 'Doe' }) .then(function (response) { console.log(response.data); }) .catch(function (error) { console.log(error); });
The above code sends a POST request and passes a JSON object as the request body.
3. Combining Vue with Axios
Now, let us combine Vue with Axios.
3.1 Install Vuex
Vuex is a state management pattern developed specifically for Vue.js applications. Install Vuex using the following command:
npm install vuex
or
yarn add vuex
3.2 Create store
Add the following code in the JavaScript file:
import Vue from 'vue'; import Vuex from 'vuex'; import axios from 'axios'; Vue.use(Vuex); const store = new Vuex.Store({ state: { users: [] }, mutations: { setUsers(state, users) { state.users = users; } }, actions: { getUsers({ commit }) { axios.get('/api/users') .then(function (response) { commit('setUsers', response.data); }) .catch(function (error) { console.log(error); }); } } }); export default store;
The above code creates a Vuex store, where state defines a state named users, mutations defines a method named setUsers for updating the value of users, and actions defines a method named getUsers for sending GET requests and updating users value.
3.3 Use store
Add the following code in the JavaScript file:
import Vue from 'vue'; import store from './store'; new Vue({ el: '#app', store, mounted() { this.$store.dispatch('getUsers'); }, computed: { users() { return this.$store.state.users; } } });
The above code will import the store, use the store in the Vue instance, and call it in the mounted hook The getUsers method saves the obtained data to users, and then binds users to the component's template through computed properties.
Now, refresh the page and you will see the user data obtained from the background.
Conclusion
Through this tutorial, you have learned to use Vue and Axios to build a simple front-end application. I hope this article will be helpful to you and give you a deeper understanding of the usage of Vue and Axios, as well as their application scenarios in front-end development. I wish you happy learning and fun with front-end development!
The above is the detailed content of Vue and Axios tutorials that even beginners can learn to help you get started with front-end development. For more information, please follow other related articles on the PHP Chinese website!